Javascript form verification (Part 1)

Source: Internet
Author: User

1. length limit:

<Script>
Function test ()
{
If (document. a. B. value. length> 50)
{
Alert ("cannot exceed 50 characters! ");
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 are allowed:

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


3. English only:

﹤ script language=java script ﹥
function onlyEng()
{
if(!(event.keyCode﹥=65&&event.keyCode﹤=90))
event.returnvalue=false;
}
﹤/ script ﹥

﹤input onkeydown="onlyEng();"﹥

4. Only numbers are allowed:

<Script language = java script>
Function onlyNum ()
{
If (! (Event. keyCode >=48 & event. keyCode <= 57)

| (Event. keyCode >=96 & event. keyCode <= 105 )))
// Consider the numeric keys on the keypad
Event. returnvalue = false;
}
</Script>

<Input onkeydown = "onlyNum ();">


5. Only English characters and numbers are allowed:

﹤input onkeyup="value="/oblog/value.replace(/[\W]/g,"'')

"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').

replace(/[^\d]/g,''))"﹥


6. Verify the email format:


﹤ script LANGUAGE=java script 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. Shielding keywords (here, shielding ***** and ****):

﹤ script language="java script 1.2"﹥
function test() {
if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ("****") == 0))

{
alert(":)");
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. Whether the two passwords are the same:

﹤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 ﹥

  1. How to dynamically change the image size through JavaScript
  2. Comprehensive list of practical JavaScript development tools
  3. Programming Language ranking in June: JavaScript is just getting started
  4. The essence of JavaScript Language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.