Preface: In front page use, encountered the validation of the date format, began to use a form control validation, but not compatible!! And the use is also subject to great constraints, so decided to write their own native JS. In order to complete the validation of the date format, the first step, of course, is to learn to use regular expressions. Used to be directly copied using, this understanding of the principle of their native write.
Next two: JS use case.
Detect control content Content format-control to "2014-07-21".
Functioncheckdate (formcontorid,message) {
Gets the control object.
var temp = document.getElementById (Formcontorid);
Gets the value of the control object.
var str = temp.value;
Handle the name of the control. Causes double quotes to be displayed.
Message = "" "+message+" "";
if (! /^ (19|20) \d{2}\-(0[1-9]|1[012]) \-(0[1-9]|1\d{1}|2\d{1}|3[0-1]) $/gi.test (str)) {
Alert (message+ "format error. Please enter a format such as "2014-01-01". The range is "1900-01-01" to "2099-12-31". ");
Temp.focus ();
return false;
}
return true;
}
One way to declare regular expression literals in regular expressions
Copy the Code code as follows:
var re =/a/gi;
Regular expression-related methods and properties
methods for regular expression objects
? Test, returns aBooleanvalue that indicates whether the pattern exists in the string being searched for. Returns if it existstrue, or you returnfalse.
If you have good suggestions, you can leave a message or send to the author e-mail: [Email protected]
Regular Expressions--js Use cases