View Full Version : Javascript code for validation

03-05-2012, 07:45 PM
Hello Everyone

I am new on java script and I want to check the form blank text fields by using onChange event.

Please guide me, is it possible or not....If possible then please send me the code.

MY EMAILID IS dhiman.anurag1@gmail.com


Philip M
03-05-2012, 08:02 PM
Form validation of the pattern if (document.forms[0].elements[1].value == "") (that is, checking simply for a blank field) is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

For example, this is how you would validate an email address:-


Email address <input type = "text" name = "email" size = "45" onblur = "testEmail(this)">
<span id = "message" style="display:none; color:red;"> The email address is invalid - please retype it</span>


<script type = "text/javascript">
function testEmail(which) {
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(which.value))) {
which.value = "";
setTimeout("document.forms[0].email.focus()", 25);
return false;
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.