Aakash Sahai
Aakash Sahai

Reputation: 4004

javascript validation

I am using a regular expression validation to validate my form using javascript. my pattern is

/^[a-zA-Z]+[a-zA-Z0-9_]*\\.{0.1}[a-zA-Z0-9_]*/

is this pattern correct for validating "not starting from number","only 1 '.' allowed and then text"?

wat can i use in javascript for validation?? something like preg_match func of php.

I m not familiar with regular expressions so help me and give sum func to do this for me. i tried to use regexp object but cant get result from .exec,test function.

Upvotes: 1

Views: 188

Answers (1)

Kissaki
Kissaki

Reputation: 9257

Yes, the regexp looks good. Require 1 char, followed by 0..inf char, number or _, followed by an optional dot, followed by 0..inf char, number or _.

You have one syntacticaly error though. Replace the {0.1} with {0,1}.

You can then use the .test() function and pass it a string to test.

var a = "bb";
var r = /^[a-zA-Z][a-zA-Z0-9_]*\.{0,1}[a-zA-Z0-9_]*/;
r.test(a);

returns true.

Upvotes: 2

Related Questions