Home  >  Article  >  Web Front-end  >  Collection of 28 JS verification functions_form effects

Collection of 28 JS verification functions_form effects

WBOY
WBOYOriginal
2016-05-16 18:33:361061browse
Calling method and attention of JS verification function:
1. Set onsubmit="return test()" to the form and cooperate with to submit.
2. //When Press the mouse to directly call the function verification
3. //Pass in your own value for verification, test(this)//Pass in the object itself for verification
4. //Click Btn Verify, if passed, use document.forms[0].submit(); to submit the form
5.//Use submit for submission verification, if return false in test; then do not submit and return true for submission.
6.

7.||Not with, && or,!
8. Common events: onblur loses focus, onchange loses focus and the content changes, onfocus element gains focus, onreset when the form When the RESET attribute is activated, this event is triggered when the onsubmit form is submitted
9. Verify the regular expression: if (/^[1-9]d*$/.test(str)) returns true and passes, FALSE does not access the value of the ID through
10.document.getElementById("ip").value//Access the value of the ID through document.form1.text1.value//Access through name11.
//Execute a single item Regular verification example
Verification function:

1. Character length limit
function test()
{
if(document.form1.text1.value.length>50 )
{
alert("Cannot exceed 50 characters!");
document.form1.text1.focus();
return false;
}
}
2. It can only be English, letters or numbers
function test()
{ if(!(event.keyCode>=65&&event.keyCode{
alert( "It can only be in English!");
document.form1.text1.focus();
}
}
//Letters or numbers
3. It can only be numbers
function test()
{ if(!((event.keyCode>=48&&event.keyCode=96&&event.keyCode{
alert("It can only be numbers! ");
document.form1.text1.focus();
}
}
or
function test(NUM)
{
var i,j,strTemp;
strTemp="0123456789";
if ( NUM.length== 0)
return 0 for (i=0;i{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
alert("It can only be a number!");
return false;
}
}
//The description is a number
return true;
}
5. Verify email (regular, function)
function isEmail(strEmail) {
if (strEmail.search(/^w ((-w )|(.w ))*@[A-Za-z0-9] ((.|-)[A-Za-z0-9] )*.[A -Za-z0-9] $/) != -1)
return true;
else
alert("Format error!");
}
function isEmail() {
if (document.userinfo.useremail.value.charAt(0)=="." ||
document.userinfo.useremail.value.charAt(0)=="@"||
document. userinfo.useremail.value.indexOf('@', 0) == -1 ||
document.userinfo.useremail.value.indexOf('.', 0) == -1 ||
document. userinfo.useremail.value.lastIndexOf("@")==document.userinfo.useremail.value.length-1 ||
document.userinfo.useremail.value.lastIndexOf(".")==document.userinfo. useremail.value.length-1)
{
alert("The email address format is incorrect! ");
document.userinfo.useremail.focus();
return false;
}
}

6. Block keywords (block *** here and ****)
function test() {
if((document.form1.text1.value.indexOf ("***") == 0)||(document.form1.text1.value .indexOf ("****") == 0)){
alert("keyword exists");
document.form1.text1.focus();
return false;
}
}
7. Compare the two inputs to see if they are the same
if(document.userinfo.userpassword.value != document.userinfo.userpassword1.value) {
document.userinfo.userpassword.focus( );
document.userinfo.userpassword.value = '';
document.userinfo.userpassword1.value = '';
alert("The passwords entered twice are different, please re-enter!");
return false;
}
8. Determine whether it is empty or space composed
function test(){
if(checkspace(document.form1.text1.value)) {
document.form1.text1.focus(); alert("It is empty or contains spaces!");
return false;
}
}
function checkspace(checkstr) {
var str = '';
for(i = 0; i str = str ' ';
}
return (str == checkstr);
}
Or:
//Execute a single regular verification example
9. Verify whether it is a digital phone call, which can only be numbers and -
Regular: d{3}-d{8}|d{4}-d{7}
function istel(elem) {
var str=elem.value;
var oneDecimal=false;
var oneChar=0;
str=str.toString( );
for (var i=0; ioneChar=str.charAt(i).charCodeAt(0);
if(oneChar==45){ continue; }
if(oneChar 57) {
alert("Only numbers and '-' signs can be entered for this item.");
return false;
}
}
return true;
}
or
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
alert("Only numbers and '- can be entered in this item 'No.");
return false;
}
}
//Explanation is legal
return true;
}
or
//Verify ordinary phone number , Fax number: It can start with " ", and in addition to numbers, it can contain "-"
function isTel(s)
{
//var patrn=/^[ ]{0,1}(d) {1,3}[ ]?([-]?(d){1,12}) $/;
var patrn=/^[ ]{0,1}(d){1,3}[ ] ?([-]?((d)|[ ]){1,12}) $/;
if (!patrn.exec(s)) return false
return true
}
Or
requirements:
(1) The phone number consists of numbers, "(", ")" and "-"
(2) The phone number is 3 to 8 digits
(3) If the phone number If the number contains an area code, then the area code is three or four digits
(4) The area code is separated from other parts by "(", ")" or "-"
(5) The mobile phone number is 11 or 12 digits, if it is 12 digits, then the first digit is 0
(6) The first and second digits of the 11-digit mobile phone number are "13"
(7) The first digit of the 12-digit mobile phone number The second and third digits are "13"
According to these rules, the following regular expressions can be produced:
(^[0-9]{3,4}-[0-9]{3 ,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^ 0{0,1}13[0-9]{9}$)
function PhoneCheck(s) {
var str=s;
var reg=/(^[0-9]{3 ,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9 ]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
alert(reg.test(str));
}

10.//When opt2 is 1, check whether num is a negative number //When opt1 is 1, check whether num is a decimal number //Returning 1 is correct, 0 is wrong
function chknbr(num ,opt1,opt2)
{
var i=num.length;
var staus;
//staus is used to record the number of .
status=0;
if ( (opt2!=1) && (num.charAt(0)=='-'))
{
alert("You have entered an invalid number.");
return 0;
}
//An error occurs when the last digit is .
if (num.charAt(i-1)=='.')
{
alert("You have entered an invalid number. ");
return 0;
}
for (j=0;j{
if (num.charAt(j)=='.')
{
status ;
}
if (status>1)
{
alert("You have entered an invalid number.");
return 0;
}
if (num.charAt(j)'9' )
{
if (((opt1==0) || (num.charAt(j)!='.') ) && (j!=0))
{
alert("You have entered an invalid number.");
return 0;
}
}
}
return 1;
}
11. Check whether it is a string of numbers or letters
function test(str)
{
var strSource ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var ch;
var i;
var temp;
for (i=0;i{
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp==-1)
{
alert("Incorrect format!");
return false;
}
}
if (strSource.indexOf(ch)==-1)
{
alert("Incorrect format!");
return false;
}
else
{
return true;
}
}
12. Numeric verification
Positive integer verification^[1-9]d*$, negative integer verification^-[1-9] d*$, integer^-?[1-9]d*$, non-negative integer^[1-9]d*|0$, non-positive positive number^-[1-9]d*|0$, float Points^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0 |0)$
function test(str){
if( /^[1-9]d*$/.test(str))
{
alert("Invalid format!");
return false;
}
else
{
return true;
}
}
13. ID card
regular: d{15}|d{18}
14. IP address
regular: d. d .d .d
15. Postal code
Regular: [1-9]d{5}(?!d)
function isPostalCode(s)
{
//var patrn =/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9]{3,12}$/;
if ( !patrn.exec(s)) return false
return true
}
16.QQ number
regular: [1-9][0-9]{4,}
17. HTML tag
regular: d{3}-d{8}|d{4}-d{7}
18. Is the color value valid?
function IsColor(color){
var temp =color;
if (temp=="") return true;
if (temp.length!=7) return false;
return (temp.search(/\#[a-fA-F0 -9]{6}/) != -1);
}
19. Is the link valid?
function IsURL(url){
var sTemp;
var b=true;
sTemp=url.substring(0,7);
sTemp=sTemp.toUpperCase();
if ((sTemp!="HTTP://")||(url.lengthb=false;
}
return b;
}
20. Is the mobile phone number valid?
function IsMobile(_str){
var tmp_str = trim(_str);
var pattern = /13\d{9}/;
return pattern .test(tmp_str);
}
or
//Verify mobile phone number: must start with a number. In addition to numbers, it can contain "-"
function IsMobile(s)
{
var patrn=/^[ ]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12}) $/;
if (!patrn.exec(s)) return false
return true
}
21.IP address and ID number verification (regular)
function checkIP()
{
obj=document.getElementById("ip").value
//ip address
//var exp=/^(d{1,2}|1dd|2[0-4]d|25[ 0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d| 25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/;
//ID card
//var exp =/b(((?!ddd)d |1dd|2[0-4]d|25[0-5])(b|.)){4}/
//var exp=/^( d{15}|d{17}[x0-9])/
//var exp=/^([d]{15}|[d]{18}|[d]{17}[x| X])$/
var reg = obj.match(exp);
if(reg==null)
{
alert("IP address is illegal!");
}
else
{
alert("IP address is legal!");
}
}
22. Verify login name: only 5-20 characters starting with letters can be entered. Strings that can contain numbers, "_", and "."
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA- Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
23. Verify user Name: You can only enter 1-30 strings starting with letters
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}
24. Verify password: only 6-20 letters, numbers, and underscores can be entered
function isPasswd (s)
{
var patrn=/^(w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}
25. Date and time class
Short time, in the form of (13:04:06)
function isTime(str)
{
var a = str.match(/^(d{ 1,2})(:)?(d{1,2})2(d{1,2})$/);
if (a == null) {alert('The input parameter is not in time format '); return false;}
if (a[1]>24 || a[3]>60 || a[4]>60)
{
alert("The time format is incorrect") ;
return false
}
return true;
}
Short date, in the form of (2003-12-05)
function strDateTime(str)
{
var r = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);
if(r= =null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&& (d.getMonth() 1)==r[3]&&d.getDate()==r[4]);
}
A long time, in the form of (2003-12-05 13:04:06 )
function strDateTime(str)
{
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)return false;
var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth() 1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5 ]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
26. Determine whether the characters are all composed of a-Z or A-Z letters

27. The judging characters are composed of letters, numbers, underscores, and periods. And the beginning can only be underscores and letters

28. The value of the form cannot be empty and cannot exceed imax Characters, cannot be less than imix characters, the input is Chinese judgment
function isNull(elem){
//var pattern=/^s |s $/;
if(elem.replace(/(^s |s$)/g, "")==""){
return false;
}else{
return true;
}
}
function imax(elem) {
if(elem.length>imax){
return false;
}else{
return true;
}
}
function imix(elem){
if(elem.lengthreturn false;
}else{
return true;
}
}
function isChinese(elem){
var pattern=/[^x00-xff] /g;
if(pattern.test(elem)){
//Contains Chinese
return false;
}else{
//Does not contain Chinese
return true;
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn