Rama Rao M
Rama Rao M

Reputation: 3051

Regular expression to check contains only

EDIT: Thank you all for your inputs. What ever you answered was right.But I thought I didnt explain it clear enough.

I want to check the input value while typing itself.If user is entering any other character that is not in the list the entered character should be rolled back.

(I am not concerning to check once the entire input is entered).

    I want to validate a date input field which should contain only characters 0-9[digits], -(hyphen) , .(dot), and /(forward slash).Date may be like 22/02/1999 or 22.02.1999 or 22-02-1999.No validation need to be done on either occurrence or position. A plain validation is enough to check whether it has any other character than the above listed chars. [I am not good at regular expressions.]

Here is what I thought should work but not.

  var reg = new RegExp('[0-9]./-');

Here is jsfiddle.

Upvotes: 1

Views: 4628

Answers (3)

Jerry
Jerry

Reputation: 71538

If you are not concerned about the validity of the date, you can easily use the regex:

^[0-9]{1,2}[./-][0-9]{1,2}[./-][0-9]{4}$

The character class [./-] allows any one of the characters within the square brackets and the quantifiers allow for either 1 or 2 digit months and dates, while only 4 digit years.

You can also group the first few groups like so:

^([0-9]{1,2}[./-]){2}[0-9]{4}$

Updated your fiddle with the first regex.

Upvotes: 2

Felix Kling
Felix Kling

Reputation: 816384

Your expression only tests whether anywhere in the string, a digit is followed by any character (. is a meta character) and /-. For example, 5x/- or 42%/-foobar would match.

Instead, you want to put all the characters into the character class and test whether every single character in the string is one of them:

var reg = /^[0-9.\/-]+$/
  • ^ matches the start of the string
  • [...] matches if the character is contained in the group (i.e. any digit, ., / or -).
    The / has to be escaped because it also denotes the end of a regex literal.
    - between two characters describes a range of characters (between them, e.g. 0-9 or a-z). If - is at the beginning or end it has no special meaning though and is literally interpreted as hyphen.
  • + is a quantifier and means "one or more if the preceding pattern". This allows us (together with the anchors) to test whether every character of the string is in the character class.
  • $ matches the end of the string

Alternatively, you can check whether there is any character that is not one of the allowed ones:

var reg = /[^0-9.\/-]/;

The ^ at the beginning of the character class negates it. Here we don't have to test every character of the string, because the existence of only character is different already invalidates the string.

You can use it like so:

if (reg.test(str)) { // !reg.test(str) for the first expression
    // str contains an invalid character
}

Upvotes: 6

Giovanni P.
Giovanni P.

Reputation: 1047

Try this:

([0-9]{2}[/\-.]){2}[0-9]{4}

Upvotes: 3

Related Questions