0

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.

1
  • I'm not quite clear on what you want to achieve with the regular expression, but I advise you to check out regextester.com which I use to verify my "regexes", as well as take a look at regular-expressions.info/reference.html, as there are probably a few character classes, that will make it easier to read your expression Commented Feb 2, 2011 at 11:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hey thanks for your support but..this validation pattern not working perfectly...i used '$' at the end then its working perfectly..May i know what is the use of '$' at the end..i used it eariler but didnt asked y it is used..
the first 'plus' is unnecessary
$ marks the end of a string. Thus, if you include it in your regexp it will require that the string ends there. This is helpful if you want to specify specific lengths as well etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.