JavaScript is often used for instance summaries of validation functions

  • 2020-03-30 04:22:40
  • OfStack

This example summarizes common JavaScript validation functions. Share with you for your reference. The detailed summary is as follows:

One, string class validation

1. Length limitation

<script>
function test()
{
if(document.a.b.value.length>50)
{
alert(" No more than 50 A character! ");
document.a.b.focus();
return false;
}
}
</script>
<form name=a onsubmit="return test()">
<textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
<input type="submit" name="Submit" value="check">
</form>

2. Only Chinese characters

<input onkeyup="value="/oblog/value.replace(/[^/u4E00-/u9FA5]/g,'')">

"Only in English

<script language=javascript>
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>
<input onkeydown="onlyEng();">

Only Numbers

<script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//Consider the number keys on the keypad
event.returnvalue=false;
}
</script>
<input onkeydown="onlyNum();">

5. English characters and Numbers only

<input onkeyup="value="/oblog/value.replace(/[/W]/g,"'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">

Verify the mailbox format

<SCRIPT LANGUAGE=javascript RUNAT=Server>
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("oh");
}
</SCRIPT>
<input type=text onblur=isEmail(this.value)>

7. Block keywords (block *** and **** here)

<script language="javascript1.2">
function test() {
if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ("****") == 0)){
alert("123");
a.b.focus();
return false;}
}
</script>
<form name=a onsubmit="return test()">
<input type=text name=b>
<input type="submit" name="Submit" value="check">
</form>

8. Enter the same password twice

<FORM METHOD=POST ACTION="">
<input type="password" id="input1">
<input type="password" id="input2">
<input type="button" value="test" onclick="check()">
</FORM>
<script>
function check()
{
with(document.all){
if(input1.value!=input2.value)
{
alert("false")
input1.value = "";
input2.value = "";
}
else document.forms[0].submit();
}
}
</script>

9. Blocking right buttons is cool!

oncontextmenu="return false" ondragstart="return false" onselectstart="return false"

Add in the body

2. Form validation

1 form item cannot be empty

<script language="javascript">
<!--
function CheckForm()

if (document.form.name.value.length == 0) { 
alert(" Please enter your name !");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>

2. Compare the values of the two form items to see if they are the same

<script language="javascript">
<!--
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) { 
alert(" The password you entered is not the same! Please re-enter .");
document.ADDUser.PWD.focus();
return false;
}
return true;
}
-->
</script>

Form items can be numeric and "_" only, used for phone/bank account verification, can be extended to domain name registration, etc

<script language="javascript">
<!--
function isNumber(String)

var Letters = "1234567890-"; //You can add your own input value
var i;
var c;
if(String.charAt( 0 )=='-')
return false;
if( String.charAt( String.length - 1 ) == '-' )
return false;
for( i = 0; i < String.length; i ++ )

c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()

if(! isNumber(document.form.TEL.value)) { 
alert(" Your phone number is illegal! ");
document.form.TEL.focus();
return false;
}
return true;
}
-->
</script>

Enter a value/length limit for the form item

<script language="javascript">
<!--
function CheckForm() 

if (document.form.count.value > 100 || document.form.count.value < 1)

alert(" The input value cannot be less than zero or greater 100!");
document.form.count.focus();
return false;
}
if (document.form.MESSAGE.value.length<10)

alert(" Input text less than 10!");
document.form.MESSAGE.focus();
return false;
}
return true;
}
//-->
</script>

5 Chinese/English/number/email address legitimacy judgment

<SCRIPT LANGUAGE="javascript">
<!--
function isEnglish(name) //English value detection

if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) { 
if(name.charCodeAt(i) > 128)
return false;
}
return true;
} function isChinese(name) //Chinese value detection

if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) { 
if(name.charCodeAt(i) > 128)
return true;
}
return false;
} function isMail(name) //The E-mail value detects

if(! isEnglish(name))
return false;
i = name.indexOf(" at ");
j = name dot lastIndexOf(" at ");
if(i == -1)
return false;
if(i != j)
return false;
if(i == name dot length)
return false;
return true;
} function isNumber(name) //Value detection

if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) { 
if(name.charAt(i) < "0" || name.charAt(i) > "9")
return false;
}
return true;
} function CheckForm()

if(! isMail(form.Email.value)) { 
alert(" Your email is illegal! ");
form.Email.focus();
return false;
}
if(! isEnglish(form.name.value)) { 
alert(" English name illegal! ");
form.name.focus();
return false;
}
if(! isChinese(form.cnname.value)) { 
alert(" Chinese name is illegal! ");
form.cnname.focus();
return false;
}
if(! isNumber(form.PublicZipCode.value)) { 
alert(" Zip code is illegal! ");
form.PublicZipCode.focus();
return false;
}
return true;
}
//-->
</SCRIPT>

Restrict the characters that form items cannot enter

<script language="javascript">
<!-- function contain(str,charset)//The string contains the test function

var i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
} function CheckForm()

if ((contain(document.form.NAME.value, "%/(/)><")) || (contain(document.form.MESSAGE.value, "%/(/)><")))

alert(" An illegal character was entered ");
document.form.NAME.focus();
return false;
}
return true;
}
//-->
</script>

Iii. Other verification:

Check if a string is composed entirely of Numbers & PI;

<script language="Javascript"><!--
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>

 
2. How to tell if it's a character
if (/[^/x00-/xff]/g.test(s)) alert(" Contains Chinese characters ");
else alert(" All characters ");

 
How to tell if it contains Chinese characters & cake;  
if (escape(str).indexOf("%u")!=-1) alert(" Contains Chinese characters ");
else alert(" All characters ");

     
4. Mailbox format verification      
//Function name: chkemail
//Check Email Address
//Parameter description: to check the string
//Return value: 0: not 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      
//Function name: fucCheckNUM
//Check if the number is
//Parameter description: the number to check
//Return value: 1 is a number, 0 is not a number
function fucCheckNUM(NUM)
{
var i,j,strTemp;
strTemp="0123456789";
if ( NUM.length== 0)
return 0
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//Indicates that a character is not a number
return 0;
}
}
//The number is
return 1;
}

     
6. Phone number format verification      
//Function name: fucCheckTEL
//Check for phone number
//Parameter description: to check the string
//Return value: 1 is legal, 0 is illegal
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i<TEL.length;i++)
{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
//Indicates an invalid character
return 0;
}
}
//
return 1;
}

   
7. Determines whether the input is a Chinese function    
function ischinese(s){   
var ret=true;  
for(var i=0;i<s.length;i++)  
ret=ret && (s.charCodeAt(i)>=10000);  
return ret;  
}

   
8. Comprehensive function & PI to judge the validity of user input;
<script language="javascript">  
//Limit the number of digits the input character begins & NBSP; < br / > //M is the user input, n is the number of digits to be restricted & NBSP; < br / > function issmall(m,n) 

if ((m<n) && (m>0)) 
 { 
 return(false); 
 } 
else 
{return(true);} 
}

 
9. Determine if the password is typed consistently  
function issame(str1,str2)  

if (str1==str2) 
{return(true);} 
else 
{return(false);} 
}

 
10. Determine if the user name is an alphanumeric slider
function notchinese(str){
var reg=/[^A-Za-z0-9_]/g
   if (reg.test(str)){
   return (false);
   }else{
return(true);   }
}

11. General validation function for form text field
 
Action: detects all input text that must not be empty, such as name, account number, email address, etc.
This validation is now only for the text field, and you can change the criteria if you want to target other field objects in the form.

How to use: add title text to the text field to be detected. The text is in the prompt message, you want to prompt the user to the Chinese name of the field. Let's say I want to detect the user name
HTML below < Input name="txt_1" title=" name "> Of course, it's best to edit the field with a visual tool like dreamweaver.
If you want to detect numeric type data, then unify the id of the field to sz.
Javascript to determine the date type is more difficult, so there is no date type validation procedures.

Program comparison grass, just to provide an idea. Lead a brick to a stone! :)
Oh yes, function call method: < The form onsubmit = "return dovalidate ()" >

function dovalidate()
{
fm=document.forms[0] //Detect only one form, if more than one can change the judgment condition
   for(i=0;i<fm.length;i++)
   { 
   //Detect the judgment conditions, depending on the type can be modified
   if(fm.tagName.toUpperCase()=="INPUT" &&fm.type.toUpperCase()=="TEXT" && (fm.title!=""))
   
   if(fm.value="/blog/="")//
   {
   str_warn1=fm.title+" Can't be empty !";
   alert(str_warn1);
  fm.focus();
   return false;   
   }
   if(fm.id.toUpperCase()=="SZ")//Number check
   {
 if(isNaN(fm.value))
{ str_warn2=fm.title+" Format is wrong ";
alert(str_warn2);
fm.focus();
 return false;
 }
  }
   }
   return true;
}

12. Verify that the radio is selected

<script language="javascript">
function checkform(obj)
{
for(i=0;i<obj.oo.length;i++)
if(obj.oo[i].checked==true) return true; alert(" Please select a ")
return false;   }
</script>
<form id="form1" name="form1" method="post" action=""    onsubmit="return checkform(this)">
    <input type="radio" name="oo" value="radiobutton" />
    <input type="radio" name="oo" value="radiobutton" />
    <input type="submit" name="Submit" value=" submit " />
</form>


Related articles: