Monday, April 6, 2009

How to use regular expression in javaScript

Hi,
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 www.javascriptkit.com/ 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 (document.myform.myinput.value.search(USPhoneFormat)==-1) //if match failed
alert("Please enter a valid format")
}
[/script]

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

[/form]

Thanks
HelpOnDesk

No comments: