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.
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.
Share Improve this question edited Aug 21, 2013 at 13:40 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Aug 21, 2013 at 13:15 Rama Rao MRama Rao M 3,07111 gold badges48 silver badges68 bronze badges 3- After your edit: possible duplicate of Is it possible to block a certain character or group of characters from entering into text box or any input field using JQUERY? – Felix Kling Commented Aug 21, 2013 at 13:41
- But that post answer does check only chars but not symbols like ./- too... – Rama Rao M Commented Aug 21, 2013 at 13:46
- 1 Yeah, well, it's up to you to e up with the right expression. You seem to be asking for two things in your question now: (1) How to test whether a string contains certain characters and (2) how to prevent character input. For the expression you will find the answers below. The other issues a duplicate of the linked to question. – Felix Kling Commented Aug 21, 2013 at 13:49
3 Answers
Reset to default 6Your 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
ora-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
}
Try this:
([0-9]{2}[/\-.]){2}[0-9]{4}
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.