Instead of writing my own validation, I thought I would use jQuery, but I also don't find it easy. I have a few questions that I hope someone can answer. First, error messages only appear when you click the submit button. How can I make them appear after exiting each field? Here is my code to check.
code:
$(document).ready(function(){
$("#orderForm").validate({
rules: {
shipFirstName: {
required: true,
},
shipFamilyName: {
required: true,
},
shipPhoneNumber: {
required: true,
},
shipStreetName: {
required: true,
},
shipCity: {
required: true,
},
billEmailAddress: {
required: true,
},
billPhoneNumber: {
required: true,
},
billCardNumber: {
required: true,
},
billCardType: {
required: true,
},
},
});
});
This is my HTML code for the form. I will not show the style, but I changed it to display a red font.
code:
<form id="orderForm" method="post" action="x">
<table id="formTable" cellpadding="5">
<tr>
<td>
<h3>Shipping and Billing Information</h3>
</td>
<td> </td>
</tr>
<tr>
<td><label for="shipFirstname">First Name</label></td>
<td><input id="shipFirstName" type="text" name="shipFirstName" maxlength=
"30" /></td>
</tr>
<tr>
<td><label for="shipFamilyName">Surname</label></td>
<td><input id="shipFamilyName" type="text" name="shipFamilyName" maxlength="30" /></td>
</tr>
<tr>
<td><label for="shipPhoneNumber">Contact Telephone Number</label></td>
<td><input id="shipPhoneNumber" type="text" name="shipPhoneNumber" maxlength="30" /></td>
</tr>
<tr>
<td><label for="shipStreetName">Street Name</label></td>
<td><input id="shipStreetName" type="text" name="shipStreetName" maxlength="30" /></td>
</tr>
<tr>
<td><label for="shipCity">City</label></td>
<td><input id="shipCity" type="text" name="shipCity" maxlength="30" /></td>
</tr>
<tr>
<td><label for="shipPostalCode">Postal Code</label></td>
<td><input id="shipPostalCode" type="text" name="shipPostalCode" maxlength="30" /></td>
</tr>
<tr>
<td><label for="billEmailAddress">Email address</label></td>
<td><input id="billEmailAddress" type="text" name="billEmailAddress" maxlength="30" /></td>
</tr>
<tr>
<td><label for="billPhoneNumber">Contact Telephone Number</label></td>
<td><input id="billPhoneNumber" type="text" name="billPhoneNumber" maxlength="30" /></td>
</tr>
<tr>
<td><label for="fidelityCardNumber">Fidelity card</label></td>
<td><input id="fidelityCardNumber" type="text" name="fidelityCardNumber" maxlength="30" /></td>
</tr>
<tr>
<td><label for="billCardNumber">Credit Card Number</label></td>
<td><input id="billCardNumber" type="text" name="billCardNumber" maxlength="30" /></td>
</tr>
<tr>
<td><label for="billCardType">Credit Card Type</label></td>
<td><select id="billCardType" name="billCardType">
<option value="...">
Choose your card...
</option>
<option value="visa">
Visa
</option>
<option value="mastercard">
Mastercard
</option>
</select></td>
</tr>
<tr>
<td><label for="instructions">Instructions</label></td>
<td>
<textarea id="instructions" name="instructions" rows="8" cols="30">
Enter your requirements here or comments.
</textarea></td>
</tr>
<tr>
<td colspan="2"><input id="submit" type="submit" name="Submit" value="Submit" </td>
</tr>
</table>
</form>
I also want to use regular expressions for zip code and fidelity. How to include them in a script? It is right? Where can I say?
$.validator.addMethod('shipPostalCode', function (value) {
return /^[A-Z]{2}\d{1,2}\s\d{1,2}[A-Z]{2}$/.test(value);
}, 'Please enter a valid Postal Code');
$.validator.addMethod('fidelityCardNumber', function (value) {
return /^[A-Z]{1}([A-Z]|\d){4}\s?([A-Z]|\d){5}\s?([A-Z]|\d){3}\d{1}(\!|\&|\@|\?){1}$/.test(value);
}, 'Please enter a valid card number');
One last thing. Can this script fit easily into an external js file? I am trying to get as much as possible from an html file.
thanks