top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How we can validate our entered form data before submitting it to the web server?

0 votes
112 views

I'm trying to validate form data before submitting it to the web server but the code giving error.
I'm not able to find where is the problem in code.
so plz correct it....

<script type="text/javascript">
<!--
function validateEmail()
{

   var email_id = document.Form.emaill.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos >1 : ( dotpos - atpos > 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.EMail.focus() ;
       return false;
   }
   return( true );
}
//-->
</script>
posted Jul 17, 2014 by Vrije Mani Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Here is error in in your code.
The correct code is:---

  <script type="text/javascript">
    <!-- function validateEmail()
    {
       var emailID = document.myForm.EMail.value;
       atpos = emailID.indexOf("@");
       dotpos = emailID.lastIndexOf(".");
       if (atpos < 1 || ( dotpos - atpos < 2 )) 
       {
           alert("Please enter correct email ID")
           document.myForm.EMail.focus() ;
           return false;
       }
       return( true );
    }
    //-->
    </script>
answer Jul 30, 2014 by Deepak Negi
Similar Questions
+2 votes

I have task to validate or parse following http header

Accept
Accept-Charset
Accept-Encoding
Accept-Language
Accept-Datetime
Content-Length
Content-MD5
Content-Type

lets take Accept-Language as an example , if header language comes other than en-us I should reject the request with customized code response.

let me know ways to achieve the task? can we do with any configuration files?
We are using tomcat 6.0.18

+2 votes

I can't see what's going on behind the scenes in this example so I'm asking for help in order to understand the Strtus process.

I run the example, leave empty "First Name" to make validation fail, and submit. Then go back to index:

http://localhost:8084/form_xml_validation

and click on edit. But "First name" is reset with the original value. It should be empty because Person instance is managed statically. In fact, other examples (preparable, exclude params) behave as expected, and return an empty String for the filed left empty.

The thing here is than I'm not understanding how Struts works here. I would appreciate if you help me with this.

Example is here:
http://struts.apache.org/release/2.3.x/docs/form-validation-using-xml.html

Code for checkout here:
http://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples/

Examples doing as expected:
http://struts.apache.org/release/2.3.x/docs/exclude-parameters.html
http://struts.apache.org/release/2.3.x/docs/preparable-interface.html

+3 votes

My form is somewhat like this:-

 <form id="form1" method="post" name="form1">
 //...fields like firstname, email......
 <button type="submit" id="submit" name="submit" onclick="validate(this)">SUBMIT</button>
 < /form>

The javascript validate function:-

function validate(ref)
{
    if(document.form1.firstname.value=="")
    {
            window.alert("First Name Cannot be Blank");
            return;
    }

    // validating other fields like email... If any field is invalid return 
    document.form1.action="response"
    document.form1.submit()
} 

Now what the problem is suppose I left the firstname field blank and click submit button then windows alerts that firstname field is mandatory....and as soon as I click the OK button in alert form the form is posted...

Now question is that I assign the action attribute after validating all the fields and if any one is invalid the function returns in which case action attribute of the form is not assigned then how the form is submitted.
The form should have been submitted after all the validation tests will have been successful and controls would have reached document.form1.submit(). And what is the solution for this.

I am using Python Flask and rendering template using jinja2.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...