Copy the codeThe code is as follows:
/***************************************************************************************
* validate1.0
* The copyright of this code belongs to Ocean Studio Ocean. You can use, copy, and modify this code for non-commercial purposes, but you need to
* Retain the copyright information of this studio. If you use or modify this code for commercial purposes, please contact this studio for use permission.
*
* If you have any suggestions for this program, please email to:ocean@.
*
*
*
*
*****************************************************************************************/
//Keyboard event
<PUBLIC:ATTACH EVENT="onkeypress" ONEVENT="doEvent()" />
//Paste event
<PUBLIC:ATTACH EVENT="onpaste" ONEVENT="doEvent()" />
// Lost focus event
<PUBLIC:ATTACH EVENT="onblur" ONEVENT="doEvent()" />
<SCRIPT LANGUAGE="JScript">
//Predefined check mode
var regArray = new Array(
new Array("int+0","^\\d+$",""," Need to enter a non-negative integer, please recheck"), //Non-negative integer (positive integer + 0)
new Array("int+","^[0-9]*[1-9][0-9]*$","^\\d+$","You need to enter a positive integer, please recheck"), //Positive integer
new Array("int-0","^((-\\d+)|(0+))$","^(-|(-\\d+)|(0+))$"," Need to enter a non-positive integer, please recheck"), //Non-positive integer (negative integer + 0)
new Array("int-","^-[0-9]*[1-9][0-9]*$","^(-|(-\\d+)|(0+))$"," Negative integer needs to be entered, please recheck"), //Negative integer
new Array("int","^-?\\d+$","^-|(-?\\d+)$","Enter an integer needs to be entered, please recheck"), //Integer
new Array("double+0","^\\d+(\\.\\d+)?$","^((\\d+\\.)|(\\d+(\\.\\d+)?))$"," Need to enter a non-negative floating point number, please recheck"), //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]+)|([0-9]*[1-9][0-9]*))$","^((\\d+\\.)|(\\d+(\\.\\d+)?))$","A positive floating point number needs to be entered, please recheck"), //Positive floating point number
new Array("double-0","^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$","^(-|(-\\\d+\\.)|(0+\\.)|(-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"," Need to enter a non-positive floating point number, please recheck"), //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]+)|([0-9]*[1-9][0-9]*)))$","^(-|(-\\d+\\.?)|(-\\d+\\.\\d+))$"," Negative floating point number, please recheck"), //Negative floating point number
new Array("double","^(-?\\d+)(\\.\\d+)?$","^(-|((-?\\d+)(\\.\\d+)?)|(-?\\d+)\\.)$","A floating point number needs to be entered, please recheck"), //Floating point number
new Array("char","^[A-Za-z]+$","","You can only enter English letters, please recheck"), //A string composed of 26 English letters
new Array("upperchar","^[A-Z]+$","","You can only enter English capital letters, please recheck"), //A string composed of 26 English letters capital letters
new Array("lowerchar","^[a-z]+$","","You can only enter lowercase letters in English, please recheck"), //A string composed of 26 English letters lowercase
new Array("digitchar","^[A-Za-z0-9]+$","","","You can only enter numbers and English letters, please recheck"), //A string composed of numbers and 26 English letters
new Array("digitchar_","^\\w+$","","You can only enter numbers, English letters and underscores, please recheck"), //A string composed of numbers, 26 English letters or underscores
new Array("email","^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$","^(([\\w-]+(\\.[\\w-]+)*@[\\w-]+)*@[\\w-]+)*@[\\w-]+)*@([\\w-]+\\.)+)|([\\w-]+(\\.[\\w-]+)*@[\\w-]+)*@[\\w-]+)+)$","You need to enter the correct email address, please recheck"), //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*)?)$","You need to enter the correct url address, please recheck") //url
);
//Default event handler for controlled elements
function doEvent() {
//Get the type of triggering event
var type = ;
//Get the value of the trigger element.
var value = ;
if (type == "keypress") { //If it is a keyboard press event, get the value after the keyboard press
var keyCode = ;
if (typeof() != "undefined") { //If conversion capitalization is defined
if (keyCode >= 97 && keyCode <= 122)
keyCode = = keyCode - 32;
}
else if (typeof() != "undefined") { //If conversion lowercase is defined
if (keyCode >= 65 && keyCode <= 90)
keyCode = = keyCode + 32;
}
value += (keyCode);
}
else if (type == "paste") {
value += ("Text");
}
//If the value of the trigger element is empty, it means that the user has no input and will not accept the check.
if (value == "") return;
//If the trigger element does not have a reg attribute set, the return will not perform any checks.
if (typeof() == "undefined") return;
//If the trigger element does not define the check attribute, no check will be done in the key press and paste events
if ((type == "keypress" || type == "paste") && typeof() == "undefined") return;
//If the check mode is not passed, the error message appears
var msg = "";
//Get check mode
var reg = ;
// Regular expression object
var regExp = null;
//Looking for regular expression objects from predefined check mode
for (var i=0;i<;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];
break; //Search successfully, exit the loop
}
}
if (regExp == null) { //If the predefined check pattern is not found, it means that reg itself is a regular expression object.
if ((type == "keypress" || type == "paste") && typeof() != "undefined")
regExp = new RegExp(); // Generate regular expression objects according to user-defined regular expressions.
else
regExp = new RegExp(reg); // Generate regular expression objects according to user-defined regular expressions.
msg = "Input error, please recheck"; //Error message
}
//Check that the value of the trigger element conforms to the check mode and returns directly.
if ((value)) return;
if (type == "blur") { //If the focus is lost and the check fails, an error warning box needs to appear.
//Direction whether the user has defined the error message himself
if (typeof() != "undefined")
msg = ;
//Show error message
alert(msg);
//Regain focus back to the trigger element
();
();
}
else { //If the keyboard press or paste event and the check fails, cancel the default action.
//Cancel this keyboard press or paste operation
= false;
}
}
</SCRIPT>
Application example:
Copy the codeThe code is as follows:
<html>
<head>
<style type="text/css">
TABLE {
width:100%;
}
INPUT {
behavior:url("");
}
</style>
</head>
<body style="margin:0">
<table>
<tr><td><a href=""><img border="0" src=""></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>String composed 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 composed of 26 English letters capitalization (input lowercase letters will automatically convert to uppercase)</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 composed of 26 English letters lowercase (input uppercase letters will automatically convert to lowercase)</td>
<td><input type="text" value="" reg="lowerchar" check lower></td>
<td><input type="text" value="" reg="lowerchar" lower></td>
</tr>
<tr>
<td>String composed of numbers 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>Stand consisting of numbers, 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 enter "aaa"), custom error message</td>
<td><input type="text" value="" reg="^aaa$" regcheck="^a{1,3}$" msg="Only enter aaa" check></td>
<td><input type="text" value="" reg="^aaa$" regcheck="^a{1,3}$" msg="Only enter aaa"></td>
</tr>
</table>
<hr width="50%" style="color: #FF0000">
<div style="text-align:center;font-size:9pt">copyright 2004 © Marine Studio (<a href="mailto:ocean@">ocean@</a>)</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>