表单验证
formFree:function(){
$(".free-form").bind('submit',function(event){
event.preventDefault();//阻止submit事件(用ajax提交)
var $form=$(this);
$form.find('.error').html();//清空提示信息
//$('.free-form .error').html('');//清空提示信息
var name = $form.find('input[name=freeusername]');
var mobile = $form.find('input[name=freemobile]');
var regTest = /^1[3|4|5|7|8][0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/;
if(!name.val().length || name.val() == name.attr('data-value') ){
$form.find('.error').html('请填写姓名');
name.focus();
return false;
}
if(!mobile.val().length || mobile.val() == mobile.attr('data-value') ){
$form.find('.error').html('请填写电话');
mobile.focus();
return false;
}
if(!regTest.test( mobile.val() )){
$form.find('.error').html('电话格式不对');
mobile.focus();
return false;
}
$.ajax({
url:'http://www.17house.com/signup',
type:'POST',
data:'realname='+name.val()+'&mobile='+mobile.val()+'&source=39',
dataType:'json',
success:function(data){
if(data.status == 1){
alert(data.msg);
}else{
alert('提交成功');
}
}
})
return false;
})
}
html:
<form method="post" action="" class="free-form">
<input type="text" class="input mb20" name="freeusername" data-value="您的称呼" value="">
<input type="text" class="input" name="freemobile" data-value="您的电话" value="">
<p class="error"></p>
<input type="submit" class="freeformbtn" value="免费申请">
<p class="count">累计服务<span class="cOrange">864987</span>业主</p>
</form>