Regular-expression validation

Source: Internet
Author: User

/determine if the input content is empty
function IsNull () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length==0) {
Alert (' Sorry, the text box cannot be empty or blank! '); /Please change the text box to the name of the property you need to verify!
}
}

Determine whether the date type is a type of YYYY-MM-DD format
function IsDate () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Type that determines whether the date type is YYYY-MM-DD hh:mm:ss format
function Isdatetime () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Determine whether the date type is a type of HH:MM:SS format
function Istime ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^ ((20|21|22|23|[ 0-1]\d) \:[0-5][0-9]) (\:[0-5][0-9])? $/
if (!reg.test (str)) {
Alert ("Sorry, the date you entered is not in the correct format!"); /Please change the "date" to the name of the property you need to verify!
}
}
}

Determine if the character entered is an English letter
function Isletter ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z]+$/;
if (!reg.test (str)) {
Alert ("Sorry, the English letter type you typed is not properly formatted!"); /Please change the "English letter type" to the name of the property you need to verify!
}
}
}

Determines whether the input character is an integer
function Isinteger ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {
Alert ("Sorry, the integer type you entered is not formatted correctly!"); /Please replace the "integer type" with the name of the property you want to verify!
}
}
}

Determines whether the input character is double precision
Function Isdouble (val)
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
Reg=/^[-\+]?\d+ (\.\d+) $/;
if (!reg.test (str)) {
Alert ("Sorry, the double type you entered is not in the correct format!"); /Please replace "double type" with the name of the property you want to verify!
}
}
}


Determine if the character entered is: a-z,a-z,0-9
function isstring ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z0-9_]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determines whether the character entered is in Chinese
function Ischinese ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[\u0391-\uffe5]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input email format is correct
function Isemail ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input ZIP code (six-bit only) is correct
function Iszip ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\d{6}$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Judging the number entered is not greater than a specific number
function MaxValue ()
{
var val = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {//Determines whether a number type
if (Val>parseint (' 123 '))//"123" for the maximum value set for itself
{
Alert (' Sorry, the number you entered is out of range ');//Please change "number" to the name of the property you want to verify!
}
}
}
}
 
   
  Phone:/^ ((\d{2,3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7} (\-\d{1,4})? $/    
 Mobile:/^ (\ (\ d{2,3}\)) | (\d{3}\-))? 13\d{9}$/    
 url:/^http:\/\/[a-za-z0-9]+\.[ a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "]) *$/  &NBSP;
 idcard:/^\d{15} (\d{2}[ A-za-z0-9])? $/  &NBSP;
 qq:/^[1-9]\d{4,8}$/  < Span class= "Apple-converted-space" >&NBSP;
  Some special amount:/^ ((\d{1,3} (, \d{3}) | ( \d+)) (\.\d{2})?$/              //Description: Except " xxx    xx,xxx    xx,xxx.00 "outside the format

Provide the. Trim () property for each of the JS verification methods provided above
String.prototype.trim=function () {
Return This.replace (/(^\s*) | (   \s*$)/g, "");
}

Call:
<input type= "text" name= "str" >
<input type= "button" value= "OK" onclick= "" >//onclick Write your own JS validation function to invoke

Regular-expression validation

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.