Home >Web Front-end >JS Tutorial >Jquery exercise form validation implementation code_jquery

Jquery exercise form validation implementation code_jquery

WBOY
WBOYOriginal
2016-05-16 18:13:59859browse

Jquery practice form validation

Copy code The code is as follows:


< ;form action="" method="post" id ="myform">



< ;td>









< tr>




input type ="checkbox" id = "cbOnTheInternet" name="checkbox" value ="Online" checked="checked">Online
Travel
Watch a movie





Name:
Age:
Gender: MaleFemale
Address:
Hobbies:






Jquery code


$(document).ready(
function() {
$("#myform ").submit(function(){
var username=$("#name").val();
var age=$("#age").val();
var sex =$("input[name ='sex'][checked]").val();
var address=$("#add option[selected]").val();
var size= $("input[name='checkbox'][checked]").size();

var favoriteArray=Array(size);
$("input[name='checkbox'][ checked]").each(function(index,docEl){
favouriteArray[index]=$(this).val();// or docEl.value
});
if(username= ="")
{
alert("Sex name cannot be empty! ");
$("#name").focus();
return false;
}
if(age=="")
{
alert("Age Cannot be empty");
$("#age").focus();
return false;
}
if(size==0)
{
alert( "You haven't chosen a hobby yet!");
$("input[name='checkbox']").get(0).focus();
return false;
}
for(var i=0;ialert(favouriteArray[i]);
}
alert('Submission successful!');
});

});

Statement:
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