Monday, April 6, 2009

How to use regular expression in javaScript

Today i got a problem to validate user validation on html form.
i m strange when i face difficulty on use Regular Expression. but i got the solution
from here i explained a validation with regular expression

there are two important thing pack the validation between two forward slash.
and on the value of text box use .search(regexp) . if it is -1 that mean match is fail

[script language="JavaScript1.2"]
function checkUSPhoneFormat()
var USPhoneFormat=/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/ //regular expression defining a US format number
if ( //if match failed
alert("Please enter a valid format")

[form name="myform"]
[input type="text" name="myinput" size=15]
[input type="button" onClick="checkUSPhoneFormat()" value="check"]



No comments: