Common JS verification functions

Source: Internet
Author: User
<Script language = "JavaScript">
<! --
// Check whether it is null. If it is null, false is returned; otherwise, true is returned.
Function checkisnull (inform, inputname ){
VaR form = INFORM + "."
Eval ("temp =" + form + inputname + ". value ;");
If (temp = ""){
Alert ("remind you: cannot be blank ");
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
} Else {
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return true;
}
}
// Verify whether it is a number
Function checkisnum (inform, inputname ){
If (! Checkisnull (inform, inputname) return false;
Else {
VaR form = INFORM + "."
Eval ("temp =" + form + inputname + ". value ;");
If (isnan (temp )){
Alert ("remind you: not a number ");
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
}
Else {
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return true;
}
}
}
// Verify if it is a E-MAIL
Function checkisemail (inform, inputname ){
If (! Checkisnull (inform, inputname) return false;
Else {
VaR form = INFORM + "."
Eval ("temp =" + form + inputname + ". value ;");
If (temp. search (/^/W + (-/W +) | (/. /W +) */@ [A-Za-z0-9] + ((/. |-) [A-Za-z0-9] + )*/. [A-Za-z0-9] + $/) =-1)
{Alert ("reminder: not email ");
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
}
Else {
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return true;
}
}
}
// Verify whether it is an HTTP address
Function checkishttp (inform, inputname ){
If (! Checkisnull (inform, inputname) return false;
Else {
VaR form = INFORM + "."
Eval ("temp =" + form + inputname + ". value ;");
If (temp. search (/^ http: // ([/W-] + /.) + [/W-] + (// [/W -. //? % & =] *)? /) =-1)
{Alert ("remind you: not HTTP ");
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
}
Else {
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return true;
}
}
}
// Verify whether the mobile phone number is used
Function checkismobile (inform, inputname ){
If (! Checkisnull (inform, inputname) return false;
Else {
VaR form = INFORM + "."
Eval ("temp =" + form + inputname + ". value ;");
If (temp. Search (/^ 1 [3 | 5]/d {9} $/) =-1)
{Alert ("remind you: not a mobile phone number ");
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
}
Else {
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return true;
}
}
}
// Verify whether it is a date
Function checkisdate (inform, inputname ){
If (! Checkisnull (inform, inputname) return false;
Else {
VaR form = INFORM + ".";
Eval ("temp =" + form + inputname + ". value ;");
Stemp = temp;
Temp = "/" "+ temp + "/"";
// Use a null value to replace the delimiter to determine whether the Delimiter is a number. The original string stemp is not affected during replacement.
If (temp. indexof ('-', 0 )! =-1) {separate = "-"; flag = 0; tnum = stemp. Replace (/-/g ,"");}
Else {
If (temp. indexof ('/', 0 )! =-1) {separate = "/"; flag = 0; tnum = stemp. Replace (///g ,"");}
Else {flag = 1 ;}
}
If (flag = 1) {alert ("reminder: No separator ");
Eval (Form + inputname + ". classname = 'redinput ';");
Return false;
}
Else {
If (isnan (tnum) {alert ("remind you: the date must be a number ");
Eval (Form + inputname + ". classname = 'redinput ';");
Return false;
}
Else {
Signf = stemp. indexof (separate); // the position where the separator first appears
Subyy = stemp. substring (0, signf); // year
Signl = stemp. lastindexof (separate); // the last position of the separator
Submm = stemp. substring (signf + 1, signl); // month
Subdd = stemp. substring (signl + 1, stemp. Length); // day
If (submm. Length <2) {submm = "0" + submm;} // month of conversion
If (subdd. Length <2) {subdd = "0" + subdd;} // conversion date
If (subyy. Length = 4) {subyy = subyy. substring (2, 4 );}
// Outdate = subyy + "/" + submm + "/" + subdd;
VaR MSG, Waring;
Waring = "----- remind you: the following error occurs -----"
MSG = "";
// Monthly judgment
If (eval (submm)> 12 | eval (submm) <1) {MSG = Waring + "/N" + "the month is invalid ";}
// Daily judgment
If (MSG = "") MSG = Waring; // The moon is legal
Else MSG = MSG; // Add the above illegal information when the month is invalid
If (eval (subdd)> 31 | eval (subdd) <1) {MSG = MSG + "/N" + "the day is invalid ";}
If (eval (subdd) <= 31 ){
If (eval (submm) = 2) {If (subdd> 28) MSG = Waring + "/N" + "The February + 28 is invalid ";}
}
If (eval (submm) = 4) {If (subdd = 31) MSG = Waring + "/N" + "April> 30 is invalid ";}
If (eval (submm) = 6) {If (subdd = 31) MSG = Waring + "/N" + "June> 30 is invalid ";}
If (eval (submm) = 9) {If (subdd = 31) MSG = Waring + "/N" + "September> 30 is invalid ";}
If (eval (submm) = 11) {If (subdd = 31) MSG = Waring + "/N" + "November> 30 is invalid ";}
If (msg. length! = 0) {alert (MSG );
Eval (Form + inputname + ". classname = 'redinput ';");
Eval (Form + inputname + ". Focus ();");
Return false;
}
}
}
Eval (Form + inputname + ". classname =" + form + inputname + ". classname. Replace ('redinput ','');");
Return ture;
}
}
// Total verification functions
// Call parameter description: (formname, inputname) Where formname is the form name, inputname is the name of the input text box
Function checkform (){
If (! Checkishttp ("myform", "mytxt") return false;
}
// -->
</SCRIPT>
<Style type = "text/CSS">
. Redinput {background-color: # ff0000 ;}
</Style>
<Form ID = "myform" name = "myform" method = "Post" Action = "" onsubmit = "Return checkform ()">
<Input type = "text" name = "mytxt"/>
<Input type = "Submit" name = "Submit" value = "Submit"/>
</Form>

 

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.