The more we learn the more fun we have. Take for example the simple process of validating a form. We can save the user a lot of time validating the form on the client side and still do it without those annoying alerts that pop up with a loud DING! It is rather easy with the help of a little javascript.
Now depending on what type of data you are passing to the server with your form, you will often need to also validate your fields data on the server too. This tutorial will only look at the client side validation, leaving server side validation to another tutorial at some other time.
Lets first start with our form. We are going to do a simple form that just asks for name, email address, optional phone number, a select list for subject and text area for message. Oh, I always forget, we need a submit and clear button too. Lets start it all of with our form tag.
The form tag <form> will typically have the standard attributes of a method (get or post) and some sort of action. I often use AJAX to process my forms so the action may simply be action=”#” or action =”javascript:void();”. Knowing that we will be validating this form we are going to add one more attribute to the form element to validate the form when it is submitted and it is obviously called onsubmit. Here is the tag we will be using.
<form action=“javascript:void(0);” name=“myform”
onsubmit=”return validateForm();“>
If you look what is inside the onsubmit attribute, it is pretty straight forward, we want to return the result of the javascript function we are going to create called validateForm() so that if it is true the form will will submit and if it is false it will not send.
Next we are going to just briefly go over the other form elements. There are several other elements, but for todays tutorial we are only going to use the <input>, <select> and <textarea> elements. The one thing to remember whenever using these elements is they should always have a name and id attribute and the input element should have a type attribute as well. Here is a an example of one of the input elements (for email) and our select and textarea elements.
<label for=“name”>Name:</label>
<input type=“text” id=“name” name=“name” />
<label for=“email”>Email:</label>
<input type=“text” id=“email” name=“email” />
<label for=“phone”>Phone:</label>
<input type=“text” id=“phone” name=“phonel” />
<label for=“subject”>Subject:</label>
<select id=“subject” name=“subject”>
<option value=“Advertising Inquiry”>Advertising Inquiry</option>
<option value=“Support Inquiry”>Support Inquiry</option>
<option value=“General Inquiry”>General Inquiry</option>
</select>
<label for=“message”>Message:</label>
<textarea id=“message” name=“message” rows=“” cols=“”></textarea>
<input type=“submit” id=“submit” name=“submit” value=“Submit” />
Now that the form is created, we can move on to the javascript function to validate the form. You will want to place this javascript before the form in the page. Here is the code, I will explain it as we go through it.
<script type=“text/javascript”>
<!–//
function validateForm(){
var emailfilter =
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var phonefilter =
/\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
if(document.getElementById(“name”).value==“”){
document.getElementById(“error_msg”).childNodes[0].data =
“Please enter full name”;
document.getElementById(“error_msg”).style.display = “block”;
document.getElementById(“name”).focus();
return false;
}
The first thing we did was declare that we were going to use javascript by using the <script> tag. We then defined the function validateForm() which will have no values passed to it and use the curly brackets to hold our statements for the function.
Two regular expressions (regex) are then defined which will be used for validatig the email address and the pone number. I have created these two regex items, but you can find about any regex you could imagine at http://www.regexlib.com.
The next steps are going to be repeated in slightly different ways, so we will discuss them here in a little more detail. The first statement starts with the IF statement where we check to see if what is inside the () is true. If the statement is true it continues to process what is in the curly brackets.
The text on the right hand side of the first statement replaces the childNodes.data of the element with id=”error_message”. This is followed by style change of the element, in this example we change display:none to display:block.
To make it less confusing for the user, the next statement focuses the cursor back in the input field with id =”name”. The last item in the IF statement is return false which will stop the form from processing. The IF statement is then closed with the end curly bracket.
To be continued.
