Jquery Form Verification

Source: Internet
Author: User

 

1. Only numbers can be entered

 

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> </title>

<Style type = "text/css">

# Error

{

Padding-left: 15px;

Color: Red;

}

</Style>

<Script src = "../Scripts/jquery-1.4.1.js" type = "text/javascript"> </script>

<Script type = "text/javascript">

$ (Document). ready (function (){

$ ("# Error"). hide ();

$ ("# Button1"). click (function (){

Var $ val = $ ("# Text1"). val ();

Var code;

For (var I = 0; I <$ val. length; I ++ ){

// CharAt () gets the character string at the specified position. charCodeAt () returns the encoding of the character string.

// The ASCII value of 0 is 48, and the ASCII value of 9 is 57.

Var code = $ val. charAt (I). charCodeAt (0 );

If (code <48 | code> 57 ){

$ ("# Error"). show ();

Break;

}

Else {

$ ("# Error"). hide ();

}

}

});

});

</Script>

</Head>

<Body>

<Div>

Name: <input id = "Text1" type = "text"/> <span id = "error"> Please enter numeric </span>

</Div>

<Input id = "Button1" type = "button" value = "button"/>

</Body>

</Html>

 

2. Check whether the check box is selected

 

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> </title>

<Style type = "text/css">

# Error

{

Padding-left: 15px;

Color: Red;

}

</Style>

<Script src = "../Scripts/jquery-1.4.1.js" type = "text/javascript"> </script>

<Script type = "text/javascript">

$ (Document). ready (function (){

$ ("# Error"). hide ();

$ ("# Button1"). click (function (){

Var $ val = '';

Var count = $ ('input: checkbox: checked'). length;

If (count = 0 ){

$ ("# Error"). show ();

}

Else {

$ ("# Error"). hide ();

 

$ ("Input: checkbox"). each (function (){

If ($ (this). is (': checked ')){

$ Val + = $ (this). val ();

}

})

Alert ($ val );

}

});

 

});

</Script>

</Head>

<Body>

<Div>

<Input id = "Checkbox1" type = "checkbox" value = 'hotdogs $ 2'/> Hotdogs $2 <br/>

<Input id = "Checkbox2" type = "checkbox" value = 'chocolate $ 5'/> Chocolate $5 <br/>

<Input id = "Checkbox3" type = "checkbox" value = 'cake $ 3'/> Cake $3 <br/>

<Input id = "Checkbox4" type = "checkbox" value = 'ice $ 4'/> Ice Crime $4 <br/>

<Span id = "error"> Please select checkbox as least </span>

</Div>

<Input id = "Button1" type = "button" value = "button"/>

</Body>

</Html>

Related Article

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.