Javascript judgment on the page

Source: Internet
Author: User
Title Javascript judgment on the pageD80 (original)
Keywords Javascript


After a period of programming, I felt that these javascript programs were relatively long-used, so I sorted them out. Some of them determined whether they were null, numbers, amounts, and time. Such as those that can be written to JS, which simplifies the page much better.

 

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
<Script language = "JavaScript">
Remove Spaces
Function trim (STR ){
If (Str. charat (0) = ""){
STR = Str. Slice (1 );
STR = trim (STR );
}
Return STR;
}
Judge whether it is empty
Function isempty (pobj, errmsg ){
VaR OBJ = eval (pobj );
If (OBJ = NULL | trim (obj. Value) = ""){
If (errmsg = NULL | errmsg = "")
Alert ("input is empty! ");
Else
Alert (errmsg );
OBJ. Focus ();
Return false;
}
Return true;
}
Determine if it is a number
Function isnumber (pobj, errmsg ){
VaR OBJ = eval (pobj );
Strref = "1234567890 ";
If (! Isempty (pobj, errmsg) return false;
For (I = 0; I <obj. value. length; I ++ ){
Tempchar = obj. value. substring (I, I + 1 );
If (strref. indexof (tempchar, 0) =-1 ){
If (errmsg = NULL | errmsg = "")
Alert ("data does not meet requirements, please check ");
Else
Alert (errmsg );
If (obj. type = "text ")
OBJ. Focus ();
Return false;
}
}
Return true;
}

// Determine whether it is a number. The number can be negative.
Function isnegative (pobj, errmsg ){
VaR OBJ = eval (pobj );
Strref = "1234567890 -";
If (! Isempty (pobj, errmsg) return false;
For (I = 0; I <obj. value. length; I ++ ){
Tempchar = obj. value. substring (I, I + 1 );
If (strref. indexof (tempchar, 0) =-1 ){
If (errmsg = NULL | errmsg = "")
Alert ("data does not meet requirements, please check ");
Else
Alert (errmsg );
If (obj. type = "text ")
OBJ. Focus ();
Return false;
} Else {
If (I> 0 ){
If (obj. value. substring (I, I + 1) = "-"){
If (errmsg = NULL | errmsg = "")
Alert ("data does not meet requirements, please check ");
Else
Alert (errmsg );
If (obj. type = "text ")
OBJ. Focus ();
Return false;
}
}
}
}
Return true;
}

Determine whether it is a form of money
Function ismoney (pobj, errmsg ){
VaR OBJ = eval (pobj );
Strref = "1234567890 .";
If (! Isempty (pobj, errmsg) return false;
For (I = 0; I <obj. value. length; I ++ ){
Tempchar = obj. value. substring (I, I + 1 );
If (strref. indexof (tempchar, 0) =-1 ){
If (errmsg = NULL | errmsg = "")
Alert ("data does not meet requirements, please check ");
Else
Alert (errmsg );
If (obj. type = "text ")
OBJ. Focus ();
Return false;
} Else {
Templen = obj. value. indexof (".");
If (templen! =-1 ){
Strlen = obj. value. substring (templen + 1, obj. value. Length );
If (strlen. length> 2 ){
If (errmsg = NULL | errmsg = "")
Alert ("data does not meet requirements, please check ");
Else
Alert (errmsg );
If (obj. type = "text ")
OBJ. Focus ();
Return false;
}
}
}
}
Return true;
}

Function isleapyear (year)
{
If (Year % 4 = 0 & amp; Year % 100! = 0) | (Year % 400 = 0 ))
{
Return true;
}
Return false;
}

// Determine whether the time is correct

Function isdate (checktext ){
VaR datetime;
VaR year, month, day;
VaR gone, gtwo;
If (TRIM (checktext. Value )! = ""){
Datetime = trim (checktext. value );
If (datetime. Length = 10 ){
Year = datetime. substring (0, 4 );
If (isnan (year) = true ){
Alert ("Enter the date! Format: (yyyy-mm-dd)/n )! ");
Checktext. Focus ();
Return false;
}
Gone = datetime. substring (4, 5 );
Month = datetime. substring (5, 7 );
If (isnan (month) = true ){
Alert ("Enter the date! Format: (yyyy-mm-dd)/n )! ");
Checktext. Focus ();
Return false;
}
Gtwo = datetime. substring (7,8 );
Day = datetime. substring (8, 10 );
If (isnan (day) = true ){
Alert ("Enter the date! Format: (yyyy-mm-dd)/n )! ");
Checktext. Focus ();
Return false;
}
If (gone = "-") & (gtwo = "-")){
If (month <1 | month> 12 ){
Alert ("the month must be between 01 and 12! ");
Checktext. Focus ();
Return false;
}
If (day <1 | day> 31 ){
Alert ("the date must be between 01 and 31! ");
Checktext. Focus ();
Return false;
} Else {
If (month = 2 ){
If (isleapyear (year) & day> 29 ){
Alert ("the February date must be between 01 and 29! ");
Checktext. Focus ();
Return false;
}
If (! Isleapyear (year) & day> 28 ){
Alert ("the February date must be between 01 and 28! ");
Checktext. Focus ();
Return false;
}
}
If (month = 4 | month = 6 | month = 9 | month = 11) & (day> 30 )){
Alert ("in four, six, nine, February/n dates must be between 01 and 30! ");
Checktext. Focus ();
Return false;
}
}
} Else {
Alert ("Enter the date! Format: (yyyy-mm-dd)/n )");
Checktext. Focus ();
Return false;
}
} Else {
Alert ("Enter the date! Format: (yyyy-mm-dd)/n )");
Checktext. Focus ();
Return false;
}
} Else {
Return true;
}
Return true;
}

</SCRIPT>

<Script language = "JavaScript">
Function checked (theform ){
If (! Isempty (theform. AA, 'Enter the data') return false;
}
</SCRIPT>
</Head>
<Body>
<Form method = "Post" Action = "" name = "DD" onsubmit = "Return checked (this);">
Determine whether to input data <input type = "text" name = "AA"> <br>
Judge quantity <input type = "text" name = "BB" onchange = "Return isnumber (this,''); "> <br>

Determines the quantity. The value can be negative. <input type = "text" name = "DD" onchange = "Return isnegative (this,''); "> <br>

Determine the amount <input type = "text" name = "cc" onchange = "Return ismoney (this,''); "> <br>

Judgment time <input type = "text" name = "ee" onchange = "Return isdate (this)"> <br>
<Input type = "Submit" value = "Submit">
</Form>
</Body>
</Html>

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.