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>