Copy codeThe Code is as follows: /*************************************** **************************************** ********
* Validate1.0
* This code is copyrighted by ocean. You can use, copy, and modify it for non-commercial purposes.
* Retain the copyright information of this studio. If you use or modify this code for commercial purposes, please contact this studio for a license.
*
* If you have any suggestions for this program, please email to: ocean@forever.net.cn.
*
* Ocean Studio
* Http://www.oceanstudio.net
* Ocean (ocean@forever.net.cn) Production
**************************************** **************************************** *********/
// Keyboard Events
<PUBLIC: attach event = "onkeypress" ONEVENT = "doEvent ()"/>
// Paste the event
<PUBLIC: attach event = "onpaste" ONEVENT = "doEvent ()"/>
// Loss of focus event
<PUBLIC: attach event = "onblur" ONEVENT = "doEvent ()"/>
<Script language = "JScript">
// Predefined Check Mode
Var regArray = new Array (
New Array ("int + 0", "^ \ d + $", "", "requires a non-negative integer. Please check again "), // non-negative integer (positive integer + 0)
New Array ("int +", "^ [0-9] * [1-9] [0-9] * $", "^ \ d + $ ", "You need to enter a positive integer. Please check again"), // positive integer
New Array ("int-0", "^ (-\ d +) | (0 +) $", "^ (-| (-\ d +) | (0 +) $ "," you need to enter a non-positive integer. Please check again "), // non-positive integer (negative integer + 0)
New Array ("int-", "^-[0-9] * [1-9] [0-9] * $ ", "^ (-| (-\ d +) | (0 +) $", "you need to enter a negative integer. Please check again"), // negative integer
New Array ("int", "^ -? \ D + $ "," ^-| (-? \ D +) $ "," you need to enter an integer. Please check again "), // integer
New Array ("double + 0", "^ \ d + (\. \ d + )? $ "," ^ (\ D + \.) | (\ d + (\. \ d + )?)) $ "," You need to enter a non-negative floating point number. Please check again "), // non-negative floating point number (Positive floating point number + 0)
New Array ("double +", "^ ([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ ", "^ (\ d + \\.) | (\ d + (\\. \ d + )?)) $ "," You need to enter a Positive floating point number. Please check again "), // Positive floating point number
New Array ("double-0", "^ (-\ d + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "," ^ (-| (-\ D + \\.) | (0 + \\.) | (-\ d + (\\. \ d + )?) | (0 + (\. 0 + )?)) $ "," You need to enter a non-Positive floating point number. Please check again "), // non-Positive floating point number (negative floating point number + 0)
New Array ("double-", "^ (-([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ ", "^ (-| (-\ d + \\.?) | (-\ D + \. \ d +) $ "," enter a negative floating point number. Please check again "), // negative floating point number
New Array ("double", "^ (-? \ D +) (\. \ d + )? $ "," ^ (-| ((-? \ D +) (\. \ d + )?) | (-? \ D +) \.) $ "," you need to enter a floating point number. Please check again "), // floating point number
New Array ("char", "^ [A-Za-z] + $", "", "You can only enter English letters, please check again "), // a string consisting of 26 English letters
New Array ("upperchar", "^ [A-Z] + $", "", "You can only enter uppercase letters, please check again "), // a string consisting of 26 uppercase letters
New Array ("lowerchar", "^ [a-z] + $", "", "You can only enter lower-case letters, please check again "), // a string consisting of 26 lower-case letters
New Array ("digitchar", "^ [A-Za-z0-9] + $", "", "You can only enter numbers and English letters, please check again "), // a string consisting of a number and 26 English letters
New Array ("digitchar _", "^ \ w + $", "", "You can only enter numbers, English letters, and underscores. Please check again "), // a string consisting of digits, 26 English letters, or underscores
New Array ("email", "^ [\ w-] + (\\. [\ w-] +) * @ [\ w-] + (\\. [\ w-] +) + $ "," ^ ([\ w-] + (\\. [\ w-] + )*@?) | ([\ W-] + (\\. [\ w-] +) * @ [\ w-] +) | ([\ w-] + (\\. [\ w-] +) * @ ([\ w-] + \\.) +) | ([\ w-] + (\\. [\ w-] +) * @ [\ w-] + (\\. [\ w-] +) $ "," enter the correct email address. Please check again "), // email address
New Array ("url", "^ [a-zA-z] +: // (\ w + (-\ w + )*)(\\. (\ w + (-\ w + )*))*(\\? \ S *)? $ "," ^ ([A-zA-z] + :?) | ([A-zA-z] +:/{1, 2}) | ([a-zA-z] +: // (\ w + (-\ w +) *) | ([a-zA-z] +: // (\ w + (-\ w + )*)(\\. (\ w + (-\ w + )*))*(\\? \ S *)?) $ "," Enter the correct url address. Please check again ") // url
);
// Default event handler for controlled Elements
Function doEvent (){
// Obtain the trigger event type
Var type = window. event. type;
// Obtain the value of the trigger element.
Var value = window. event. srcElement. value;
If (type = "keypress") {// if it is a keyboard press event, obtain the value after the keyboard Press
Var keyCode = window. event. keyCode;
If (typeof (window. event. srcElement. upper )! = "Undefined") {// If the conversion is defined in upper case
If (keyCode> = 97 & keyCode <= 122)
KeyCode = window. event. keyCode = keyCode-32;
}
Else if (typeof (window. event. srcElement. lower )! = "Undefined") {// converts lowercase letters if defined
If (keyCode> = 65 & keyCode <= 90)
KeyCode = window. event. keyCode = keyCode + 32;
}
Value + = String. fromCharCode (keyCode );
}
Else if (type = "paste "){
Value + = window. clipboardData. getData ("Text ");
}
// If the value of the trigger element is null, it indicates that the user has not entered the element and will not be checked.
If (value = "") return;
// If the reg attribute is not set for the trigger element, no check is performed.
If (typeof (window. event. srcElement. reg) = "undefined") return;
// If the trigger element does not define the check attribute, do not check the key and paste events
If (type = "keypress" | type = "paste") & typeof (window. event. srcElement. check) = "undefined") return;
// Error message if the check mode is not passed
Var msg = "";
// Get the check Mode
Var reg = window. event. srcElement. reg;
// Regular Expression object
Var regExp = null;
// Find the regular expression object from the predefined Check Mode
For (var I = 0; I <regArray. length; I ++ ){
If (regArray [I] [0] = reg ){
If (type = "keypress" | type = "paste") & regArray [I] [2]! = "")
RegExp = new RegExp (regArray [I] [2]); // find the predefined Check Mode
Else
RegExp = new RegExp (regArray [I] [1]); // find the predefined Check Mode
Msg = regArray [I] [3]; // defines the predefined error message
Break; // search successful, exit the loop
}
}
If (regExp = null) {// if no predefined check mode is found, reg itself is a regular expression object.
If (type = "keypress" | type = "paste") & typeof (window. event. srcElement. regcheck )! = "Undefined ")
RegExp = new RegExp (window. event. srcElement. regcheck); // generates a regular expression object based on the user-defined regular expression.
Else
RegExp = new RegExp (reg); // generates a regular expression object based on a custom regular expression.
Msg = "input error, please check again"; // error message
}
// Check that the value of the trigger element conforms to the check mode and returns the result directly.
If (regExp. test (value) return;
If (type = "blur") {// if the focus is lost and the check fails, an error warning box is required.
// Determine whether the user has defined an error message.
If (typeof (window. event. srcElement. msg )! = "Undefined ")
Msg = window. event. srcElement. msg;
// Display the error message
Alert (msg );
// Re-aggregates the focus to trigger Elements
Window. event. srcElement. focus ();
Window. event. srcElement. select ();
}
Else {// If you press or paste an event on the keyboard and the check fails, the default action is canceled.
// Cancel pressing or pasting the keyboard.
Window. event. returnValue = false;
}
}
</SCRIPT>
Example:Copy codeThe Code is as follows: <Head>
<Style type = "text/css">
TABLE {
Width: 100%;
}
INPUT {
Behavior: url ("validate. htc ");
}
</Style>
</Head>
<Body style = "margin: 0">
<Table>
<Tr> <td> <a href = "http://www.oceanstudio.net"> </a> </td> </tr>
</Table>
<Br>
<Table style = "margin-left: 20px">
<Tr>
<Td> verification rules </td>
<Td> Real-Time Detection </td>
<Td> detection when the focus is lost </td>
</Tr>
<Tr>
<Td> non-negative integer (positive integer + 0) </td>
<Td> <input type = "text" value = "" reg = "int + 0" check> </td>
<Td> <input type = "text" value = "" reg = "int + 0"> </td>
</Tr>
<Tr>
<Td> positive integer </td>
<Td> <input type = "text" value = "" reg = "int +" check> </td>
<Td> <input type = "text" value = "" reg = "int +"> </td>
</Tr>
<Tr>
<Td> non-positive integer (negative integer + 0) </td>
<Td> <input type = "text" value = "" reg = "int-0" check> </td>
<Td> <input type = "text" value = "" reg = "int-0"> </td>
</Tr>
<Tr>
<Td> negative integer </td>
<Td> <input type = "text" value = "" reg = "int-" check> </td>
<Td> <input type = "text" value = "" reg = "int-"> </td>
</Tr>
<Tr>
<Td> integer </td>
<Td> <input type = "text" value = "" reg = "int" check> </td>
<Td> <input type = "text" value = "" reg = "int"> </td>
</Tr>
<Tr>
<Td> non-negative floating point number (Positive floating point number + 0) </td>
<Td> <input type = "text" value = "" reg = "double + 0" check> </td>
<Td> <input type = "text" value = "" reg = "double + 0"> </td>
</Tr>
<Tr>
<Td> Positive floating point number </td>
<Td> <input type = "text" value = "" reg = "double +" check> </td>
<Td> <input type = "text" value = "" reg = "double +"> </td>
</Tr>
<Tr>
<Td> non-Positive floating point number (negative floating point number + 0) </td>
<Td> <input type = "text" value = "" reg = "double-0" check> </td>
<Td> <input type = "text" value = "" reg = "double-0"> </td>
</Tr>
<Tr>
<Td> negative floating point number </td>
<Td> <input type = "text" value = "" reg = "double-" check> </td>
<Td> <input type = "text" value = "" reg = "double-"> </td>
</Tr>
<Tr>
<Td> floating point number </td>
<Td> <input type = "text" value = "" reg = "double" check> </td>
<Td> <input type = "text" value = "" reg = "double"> </td>
</Tr>
<Tr>
<Td> A string consisting of 26 English letters </td>
<Td> <input type = "text" value = "" reg = "char" check> </td>
<Td> <input type = "text" value = "" reg = "char"> </td>
</Tr>
<Tr>
<Td> A string consisting of 26 uppercase letters (lowercase letters are automatically converted to uppercase letters) </td>
<Td> <input type = "text" value = "" reg = "upperchar" check upper> </td>
<Td> <input type = "text" value = "" reg = "upperchar" upper> </td>
</Tr>
<Tr>
<Td> A string consisting of 26 lower-case letters (uppercase letters are automatically converted to lower-case letters) </td>
<Td> <input type = "text" value = "" reg = "lowerchar" check lower> </td>
<Td> <input type = "text" value = "" reg = "lowerchar" lower> </td>
</Tr>
<Tr>
<Td> A string consisting of a number and 26 English letters </td>
<Td> <input type = "text" value = "" reg = "digitchar" check> </td>
<Td> <input type = "text" value = "" reg = "digitchar"> </td>
</Tr>
<Tr>
<Td> A string consisting of digits, 26 English letters, or underscores </td>
<Td> <input type = "text" value = "" reg = "digitchar _" check> </td>
<Td> <input type = "text" value = "" reg = "digitchar _"> </td>
</Tr>
<Tr>
<Td> email address </td>
<Td> <input type = "text" value = "" reg = "email" check> </td>
<Td> <input type = "text" value = "" reg = "email"> </td>
</Tr>
<Tr>
<Td> url </td>
<Td> <input type = "text" value = "" reg = "url" check> </td>
<Td> <input type = "text" value = "" reg = "url"> </td>
</Tr>
<Tr>
<Td> Custom Rules (only "aaa" can be entered), custom error messages </td>
<Td> <input type = "text" value = "" reg = "^ aaa $" regcheck = "^ a {1, 3} $" msg = "only aaa" check> </td>
<Td> <input type = "text" value = "" reg = "^ aaa $" regcheck = "^ a {1, 3} $" msg = "Enter aaa only"> </td>
</Tr>
</Table>
<Hr width = "50%" style = "color: # FF0000">
<Div style = "text-align: center; font-size: 9pt"> copyright 2004 & copy ocean Studio (<a href = "mailto: ocean@forever.net.cn> ocean@forever.net.cn </a>) </div>
<P> </p>
<P> </p>
<P> </p>
<P> </p>
</Body>
</Html>