JavaScript provides a very useful function for client side user input validation. This function uses regular expressions for validation against the input.
If you are a PHP developer then you must have knowledge of this feature, whereas for ASP.NET 2.0 we already have the regular expression validator control which also uses similar regular expressions.
A simple example of the RegExp is as follows:
<script type="text/javascript" langusge="javascript">
function validate_phone(obj)
{
//for US phone number format: (xxx) xxx-xxxx
var RegExp=/\(\d{3}\)\ \d{3}-\d{4}/;
if(RegExp.test(obj.value))
{
return true;
}
else
{
obj.select();
obj.focus();
return false;
}
}
</script>
Note: Passing a correct regular expression is very important. You can find various regular expressions according to your need by Googling around. Also note that the regular expression must be inside the / / i.e var RegExp=/ /;
No comments:
Post a Comment