Connecting Tech Pros Worldwide Help | Site Map

RegExp validation

Andyza
Guest
 
Posts: n/a
#1: May 11 '06
The following vbscript sub checks that a value has a specific format:
first 3 characters are alphabetic: anything from a - z. The last 4
characters are numeric: anything from 0 - 9

Sub CheckString(val)
Set VerifyValue = New RegExp
VerifyValue.Pattern = "^[a-z]{3}\d{4}$"
VerifyValue.IgnoreCase = True
If VerifyValue.Test(val) = False Then
alert("Please enter a valid number.")
End If
End Sub

How do I write this in JavaScript?

Vic Sowers
Guest
 
Posts: n/a
#2: May 11 '06

re: RegExp validation



"Andyza" <andyza@webmail.co.za> wrote in message
news:1147339896.016959.301550@u72g2000cwu.googlegr oups.com...[color=blue]
> The following vbscript sub checks that a value has a specific format:
> first 3 characters are alphabetic: anything from a - z. The last 4
> characters are numeric: anything from 0 - 9
>
> Sub CheckString(val)
> Set VerifyValue = New RegExp
> VerifyValue.Pattern = "^[a-z]{3}\d{4}$"
> VerifyValue.IgnoreCase = True
> If VerifyValue.Test(val) = False Then
> alert("Please enter a valid number.")
> End If
> End Sub
>
> How do I write this in JavaScript?[/color]

function CheckString(val) {
if (!/^[a-z]{3}\d{4}$/i.test(val)) alert("Please enter a valid
number.");
}


Andyza
Guest
 
Posts: n/a
#3: May 11 '06

re: RegExp validation


> function CheckString(val) {[color=blue]
> if (!/^[a-z]{3}\d{4}$/i.test(val)) alert("Please enter a valid
> number.");
> }[/color]

Man that was fast!

And it works perfectly too! Thanks.

Bogdan Blaszczak
Guest
 
Posts: n/a
#4: May 13 '06

re: RegExp validation


Vic Sowers napisał(a):[color=blue][color=green]
>>
>>How do I write this in JavaScript?[/color]
>
>
> function CheckString(val) {
> if (!/^[a-z]{3}\d{4}$/i.test(val)) alert("Please enter a valid
> number.");
> }[/color]

or, to return boolean value

function CheckString(val,message){
return/^[a-z]{3}\d{4}$/i.test(val)||!!alert(message);
}


--
BlaTek
Closed Thread


Similar JavaScript / Ajax / DHTML bytes