Regular Expressions--js Use cases

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.