...

View Full Version : Javascript code for validation



dhiman.anurag1
03-05-2012, 06: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


........
THANK YOU

Philip M
03-05-2012, 07: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:-


<form>

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>


</form>

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum