Home > Web Front-end > JS Tutorial > body text

Summary of common JavaScript verification function examples_javascript skills

WBOY
Release: 2016-05-16 16:30:16
Original
1376 people have browsed it

The examples in this article summarize common JavaScript verification functions. Share it with everyone for your reference. The specific summary is as follows:

1. String type verification

1. Length limit

Copy code The code is as follows:
<script><br> function test()<br> {<br> if(document.a.b.value.length>50)<br> {<br> alert("Cannot exceed 50 characters!");<br> document.a.b.focus();<br> return false;<br> }<br> }<br> </script>



2. Can only be Chinese characters

Copy code The code is as follows:

3." It can only be in English

Copy code The code is as follows:

4. It can only be numbers

Copy code The code is as follows:

5. Can only be English characters and numbers

Copy code The code is as follows:

6. Verify email format

Copy code The code is as follows:

7. Block keywords (*** and **** are blocked here)

Copy code The code is as follows:




8. Are the passwords entered twice the same?

Copy code The code is as follows:





<script><br> function check()<br> {<br> with(document.all){<br> if(input1.value!=input2.value)<br> {<br> alert("false")<br> input1.value = "";<br> input2.value = "";<br> }<br> else document.forms[0].submit();<br> }<br> }<br> </script>

9. Block right click is cool!

Copy code The code is as follows:
oncontextmenu="return false" ondragstart="return false" onselectstart="return false"

Add to body

2. Form verification

1 form item cannot be empty

Copy code The code is as follows:

2 Compare whether the values ​​of two form items are the same

Copy code The code is as follows:

3 form items can only be numbers and "_", used for phone/bank account verification, and can be extended to domain name registration, etc.

Copy code The code is as follows:

4 Form item input value/length limit

Copy code The code is as follows:

5 Chinese/English/numeric/email address validity judgment

Copy code The code is as follows:

6 限定表单项不能输入的字符

复制代码 代码如下:

三、其他验证:

1. 检查一段字符串是否全由数字组成 

复制代码 代码如下:

 
2. 怎么判断是否是字符
复制代码 代码如下:
if (/[^/x00-/xff]/g.test(s)) alert("含有汉字");
else alert("全是字符");

 
3. 怎么判断是否含有汉字  
复制代码 代码如下:
if (escape(str).indexOf("%u")!=-1) alert("含有汉字");
else alert("全是字符");

   
4. 邮箱格式验证   
复制代码 代码如下:
//函数名:chkemail
//功能介绍:检查是否为Email Address
//参数说明:要检查的字符串
//返回值:0:不是 1:是
function chkemail(a)
{ var i=a.length;
var temp = a.indexOf('@');
var tempd = a.indexOf('.');
if (temp > 1) {
if ((i-temp) > 3){
if ((i-tempd)>0){
return 1;
}
}
}
return 0;
}

 
5. Digital format verification
Copy code The code is as follows:
//Function name: fucCheckNUM
//Function introduction: Check whether it is a number
//Parameter description: Number to be checked
//Return value: 1 means it is a number, 0 means it is not a number
function fucCheckNUM(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)
{
//Explain that there are characters that are not numbers
return 0;
}
}
//The description is a number
return 1;
}

 
6. Phone number format verification
Copy code The code is as follows:
//Function name: fucCheckTEL
//Function introduction: Check whether it is a phone number
//Parameter description: String to be checked
//Return value: 1 means legal, 0 means illegal
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i {
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
//Indicates that some characters are illegal
return 0;
}
}
//Explanation is legal
return 1;
}


7. Function to determine whether the input is Chinese
Copy code The code is as follows:
function ischinese(s){
var ret=true;
for(var i=0;i ret=ret && (s.charCodeAt(i)>=10000);
return ret;
}


8. Comprehensive function to judge the legality of user input
Copy code The code is as follows:





I hope this article will be helpful to everyone’s web programming based on JavaScript.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template