Form Validation - multiple fields

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

08-Apr-08 19:55
Hi,

I'm sure I'm missing something simple but I'm very new to Javascript. I'm trying to validate a simple form with name, email address, telephone number and postcode fieldswhen submit is pressed. I have worked out how to validate each field but cannot seem to tie together my functions so that it validates all of the fields when submit is pressed. for example I've written one function toc heck email which works but if I add the function to check that the name field is complete nothing works!
08-Apr-08 23:33
Can you post the URL of your form with the script, so we can take a look at the problem?


--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
09-Apr-08 10:33
This is where I am at the moment - totally confused! As I said the either function seems to work when used independently. Help!

<html>
<head>
<script type="text/javascript">

function validate_form ( )
{
valid = true;

if ( document.contact_form.shipFirstName.value == "" )
{
alert ( "Please fill in the 'Your First Name' box." );
valid = false;
}

if ( document.contact_form.shipFamilyName.value == "" )
{
alert ( "Please fill in the 'Your Family Name' box." );
valid = false;
}

return valid;
}

function validate_email(field,alerttxt)
{
with (document.contact_form.email)
{
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>

</head>




<body>

<form name="contact_form" method="post" action="#" onSubmit="return validate_form ( );">
<table cellpadding="5">
<tr>
<td><h3>Shipping Information</h3></td>
<td> </td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="shipFirstName" maxlength="30" /></td>
</tr>
<tr>
<td>Surname</td>
<td><input type="text" name="shipFamilyName" maxlength="30" /></td>
</tr>
<tr>
<td>Contact Telephone Number</td>
<td><input type="text" name="shipPhoneNumber" maxlength="30" /></td>
</tr>
<tr>
<td>Street Name</td>
<td><input type="text" name="shipStreetName" maxlength="30" /></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="shipCity" maxlength="30" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
<td><input type="submit" name="Send" value="Submit" /></td>
<td> </td>
</tr>


</table>



</form>
</body></html>
09-Apr-08 23:31
Well for one thing you have two functions called validate_form() in there. You can't have 2 functions with the same name in a script.

Just create one validate_form() function, and do all your validating in there.

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
11-Apr-08 10:04
Ok Good point to be honest I think I had got closer than that before and managed to trip myself up again. Is this closer? (NB It still doesnt work! Agh!)

<script type="text/javascript">

function validate_form ( )
{
valid = true;

if ( document.contact_form.shipFirstName.value == "" )
{
alert ( "Please fill in the 'Your First Name' box." );
valid = false;
}

if ( document.contact_form.shipFamilyName.value == "" )
{
alert ( "Please fill in the 'Your Family Name' box." );
valid = false;
}

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


//-->

</script>

I guess it is the validating within one function that I'm really struggling with at the moment.

Any advice would be muchos appreciados.
11-Apr-08 15:28
Sorry to reply to my own reply but this gives a better picture of where I am now. I have functions that will check the required fields but am not sure how to get them to work together. In teh attached code I've tried to use buttons toc heck the post code and the email fields independently (p code works and email doesnet) NB this email function works fien when I take it off this page.

If you coudl advise me how to either:

a/ get the email button to activate the email function correctly

or

b/ how to link my 3 functions together that would be great.

Really lost here!!!


<head>



<script type="text/javascript">

<!--

function validate_form ( )
{
valid = true;

if ( document.contact_form.shipFirstName.value == "" )
{
alert ( "Please fill in the 'Your First Name' box." );
valid = false;
}

if ( document.contact_form.shipFamilyName.value == "" )
{
alert ( "Please fill in the 'Your Family Name' box." );
valid = false;
}

if ( document.contact_form.pcode.value == "" )
{
alert ( "Please fill in the 'Post Code' box." );
valid = false;
}




return valid;
}




//-->

</script>


<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original code by Peter Haydon -->
<!-- peter_haydon@lineone.net -->

<!-- Begin
function postit(){ //check postcode format is valid
test = document.contact_form.pcode.value; size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") //Strip leading spaces
{test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size)== " ") //Strip trailing spaces
{test = test.substr(0,size-1);size = test.length
}
document.contact_form.pcode.value = test; //write back to form field
if (size < 6 || size > 8){ //Code length rule
alert(test + " is not a valid postcode - wrong length");
document.contact_form.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
alert(test + " is not a valid postcode - cannot start with a number");
document.contact_form.pcode.focus();
return false;
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
alert(test + " is not a valid postcode - alpha character in wrong position");
document.contact_form.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.contact_form.pcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.contact_form.pcode.focus();
return false;
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
alert(test + " is not a valid postcode - no space or space in wrong position");
document.contact_form.pcode.focus();
return false;
}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
alert(test + " is not a valid postcode - only one space allowed");
document.contact_form.pcode.focus();
return false;
}
alert("Postcode Format OK");
return true;
}
// End -->
</script>

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

alert ( "Email is valid pls press submit." );

}
</script>

</head>
<body>
<!-- Page-wide header -->
<div id="header">

</div>
<!-- Leftmost column contents -->
<div id="left">
<h2>Visit:</h2>
<ul class="navlist">
<li><a href="index.html">Home</a></li>
<li><a href="product.html">Our pans</a></li>
<li><a href="contact.html">Contact us</a></li>
<li><a href="form.html">Order</a></li>
</ul>
</div>
<!-- Main column contents -->
<div id="middle"> <!-- InstanceBeginEditable name="body" --><h2>Sample Order Information Form</h2>
<form name="contact_form" method="post" action="#" onSubmit="return validate_form ( );">
<table cellpadding="5">
<tr>
<td><h3>Shipping Information</h3></td>
<td> </td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="shipFirstName" maxlength="30" /></td>
</tr>
<tr>
<td>Surname</td>
<td><input type="text" name="shipFamilyName" maxlength="30" /></td>
</tr>
<tr>
<td>Contact Telephone Number</td>
<td><input type="text" name="shipPhoneNumber" maxlength="30" /></td>
</tr>
<tr>
<td>Street Name</td>
<td><input type="text" name="shipStreetName" maxlength="30" /></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="shipCity" maxlength="30" /></td>
</tr>
<tr>
<td>Postal Code</td>
<td><input type="text" name="pcode" maxlength="30" /></td>
<td><input type="button" name="Button" value="Check Code" onclick="postit()"></td>
</tr>
<tr>
<td><h3>Billing Information</h3></td>
<td> </td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="billFirstName" maxlength="30" /></td>
</tr>
<tr>
<td>Surname</td>
<td><input type="text" name="billFamilyName" maxlength="30" /></td>
</tr>
<tr>
<td>Email address</td>
<td><input type="text" name="billEmailAddress" maxlength="30" /></td>
<td> <input type="button" name="Button" value="Check Code" onclick="return validate_contact(contact_form);"> </td>
</tr>
<tr>
<td>Contact Telephone Number</td>
<td><input type="text" name="billPhoneNumber" maxlength="30" /></td>
</tr>
<tr>
<td>Credit Card Number</td>
<td><input type="text" name="billCardNumber" maxlength="30" /></td>
</tr>
<tr>
<td>Credit Card Type</td>
<td><select name="billCardType">
<option value="...">Choose your card...</option>
<option value="visa">Visa</option>
<option value="mastercard">Mastercard</option>
</select> </td>
</tr>
<tr>
<td>Instructions</td>
<td>
<textarea name="instructions" rows="8" cols="30">Enter your requirements here or comments.</textarea></td>
</tr>
<tr>
<td><input type="submit" name="Send" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form><!-- InstanceEndEditable --></div>



<!-- Page-wide footer -->
<div id="footer">

</div>
</body>
<!-- TemplateEnd -->
<!-- InstanceEnd --></html>



12-Apr-08 01:23
function validate_form ( )
{
valid = true;

if ( document.contact_form.shipFirstName.value == "" )
{
alert ( "Please fill in the 'Your First Name' box." );
valid = false;
}

if ( document.contact_form.shipFamilyName.value == "" )
{
alert ( "Please fill in the 'Your Family Name' box." );
valid = false;
}

if ( document.contact_form.pcode.value == "" || !postit ( document.contact_form.pcode.value ) )
{
alert ( "Please fill in the 'Post Code' box." );
valid = false;
}

if ( document.contact_form.billEmailAddress.value == "" || !validate_email ( document.contact_form.billEmailAddress.value ) )
{
alert ( "Please fill in the 'Email Address' box." );
valid = false;
}

return valid;
}


... then redo your postit() and validate_email() functions to take the string to validate as a parameter, rather than extracting the string directly from the form, and take out the alert()s from the postit() and validate_email() functions also.

In other words, you want your validate_form() function to control the flow of the validation process, with postit() and validate_email() acting as helper functions to validate specific types of data. Currently you have all 3 functions kind of "competing" to validate the form at once!

Make sense?

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
12-Apr-08 11:35
Thanks for your help Its starting to make a bit of sense. I see how you are suggesting I control the flow of validation but what do you mean by "take the string to validate as a parameter, rather than extracting the string directly from the form"? Do you meant that instead of evaluating with document.contact_form.billEmailAddress
I should identify the string? How do I do that? sorry but that means nothing to me at this stage!
My code looks like this. Could you show me what do I need to change?

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

alert ( "Email is valid pls press submit." );

}
</script>

NB I think my course has a steep learning curve!!!
14-Apr-08 06:36
Yes, that's essentially right. Pass the strings through as parameters, rather than extracting them from the form. That means you can then reuse the same data validation function to validate multiple fields if needs be.

For example:

function validate_email ( value )
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{
return false;
}
else
{
return true;
}
}

Then you'd call it from your validate_form() function as I showed above, eg:

if ( document.contact_form.billEmailAddress.value == "" || !validate_email ( document.contact_form.billEmailAddress.value ) )

etc.



--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
17-Apr-08 09:24
Brilliant It's Alive!

Thanks so much. That was the last bit of my assignemnet to put together. I just need to write a short report and I'm finished.


Thanks;-)
17-Apr-08 10:01
You're welcome. Glad you got it working

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated