Jump to content

Form Validation


najd

Recommended Posts

Hi,I have two functions to validate two inputs (name,email) in a form, the problem is that one function works, and the other doesnt work. What should I add to make them all work?!The first function:

<script type="text/javascript">function validate_required(field,alerttxt){with (field){if (value==null||value=="")  {alert(alerttxt);return false}else {return true}}}function validate_form(thisform){with (thisform){if (validate_required(fname,"Name must be filled out!")==false)  {fname.focus();return false}}}</script>

The second function:

<script type="text/javascript">function validate_email(field,alerttxt){with (field){apos=value.indexOf("@")dotpos=value.lastIndexOf(".")if (apos<1||dotpos-apos<2)   {alert(alerttxt);return false}else {return true}}}function validate_form(thisform){with (thisform){if (validate_email(email,"Not a valid e-mail address!")==false)  {email.focus();return false}}}</script>

Finally, the form:

<form action="submitpage.htm" onsubmit="return validate_form(this)"method="post"><input type="text" name="fname" size="20"><input type="text" name="email" size="30"><input type="submit" value="Submit"></form>

Note: The function which is appove the other is the one that doesnt work! means that if I but it below the other code it will work.

Link to comment
Share on other sites

Yes, both work but indiviually :) . means if I put them in the same page, then just one function will work - which is below the other code.I want them to check both inputs, not just one.

Link to comment
Share on other sites

of course it doesn't work if both are on the same page. You have 2 functions with the same name. If you have 2 functions with the same name, only one of them will ever work! how would the javascript engine have a clue which one to call if it let you have 2 functions with the same exact name!sure... the smurfs got along fine calling everything "smurf".... but it doesn't work so well in javascript....does it work if you combine the contents of your 2 validate_form() functions into 1 validate_form() function?something like this:

function validate_form(thisform){    with (thisform)    {        if (validate_required(fname,"Name must be filled out!")==false)        {            fname.focus();            return false        }        if (validate_email(email,"Not a valid e-mail address!")==false)        {            email.focus();            return false        }    }}

of course... if you are wanting to get both alerts if both fields are blank... you can't return until after you call both validate_required() and validate_email()

Link to comment
Share on other sites

Was just about to say, your code should look more like:

<script type="text/javascript">function validate_form(thisform){ with (thisform){  if ( validate_required(fname,"Name must be filled out!") == false){fname.focus();return false }  if ( validate_email(email,"Not a valid e-mail address!")==false) { email.focus();return false } }}function validate_required(field,alerttxt){with (field){if (value==null||value==""){alert(alerttxt);return false}else {return true}}}function validate_email(field,alerttxt){with (field){apos=value.indexOf("@")dotpos=value.lastIndexOf(".")if (apos<1||dotpos-apos<2) {alert(alerttxt);return false}else {return true}}}</script><form action="submitpage.htm" onsubmit="return validate_form(this)"method="post"><input type="text" name="fname" size="20"><input type="text" name="email" size="30"><input type="submit" value="Submit"></form>

Link to comment
Share on other sites

Dear sbrownii,Thank you for the explanation. I,ve got the idea. The code anyway seems to be not working._______________________Dear Blue,Thank you very much for this sweet code. It is working all fine.Best regards,Najd

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...