gpt4 book ai didi

jquery - 使用 jQuery 进行表单验证并具有一些效果

转载 作者:行者123 更新时间:2023-12-01 01:22:17 25 4
gpt4 key购买 nike

我正在使用 jQuery 创建表单验证,而且我是 jQuery 新手,因此我开始使用名称、地址、邮政编码、国家/地区、性别、选择颜色(复选框)、电话号码、电子邮件、密码、验证来创建表单验证密码字段。当我创建一些代码但它产生了一些问题,因为我无法在其他条件下收到警报。下面是我的代码

$(document).ready(function() {
var err = true;
$("#submit_Button").click(function ValidateForm() {
alert("in submit");
if ($("#name").val() == null || $("#name").val() == "") {
alert("in if");
$("#name_Err").show();
$("#name").focus();
err = false;
} else {
$("#name_Err").hide();
if (!$("#name").match(/^[a-zA-Z ]+$/)) {
alert("i m in regu");
$("#name").focus();
err = false;
}
}
});
});

CSS

  .form {
margin - left: 450 px;
width: auto;
height: 500 px
}
.class = "left_Side" {
text - align: left;
width: 80 px;
position: absolute;
padding - left: 50 px;
}

html

<html>
<body>
<form name="myForm" onsubmit="return ValidateForm();" action="" class="form">
<table id="form_Table">
<tr>
<h1 style="text-align:Left; margin-left:20px;"> Javascript Validation</h1>
</tr>
<tr>
<td class="left_Side">Name :</td>
<td><input type="text" id="name" /></td>
<td><span style="color:red; display:none" id="name_Err">Please enter your name</span></td>
<td><span style="color:red; display:none" id="na_Err">Only alphabets</span></td>
</tr>
<tr>
<td class="left_Side">Address :</td>
<td><input type="text" id="address" /></td>
<td><span style="color:red; display:none" id="address_Err">Please enter your address</span></td>
<td><span style="color:red; display:none" id="add_Err">Only alphabets</span> </td>
</tr>
<tr>
<td class="left_Side">Zip Code :</td>
<td><input type="text" id="zip_code" /></td>
<td><span style="color:red; display:none" id="zipCode_Err">Please enter your zip code</span></td>
<td><span style="color:red; display:none" id="zip_Err">Only numbers</span> </td>
</tr>
<tr>
<td class="left_Side">Country :</td>
<td><select id="country_Select">
<option value="">Select Country</option>
<option value="opt_India">India</option>
<option value="opt_Australia">Australia</option>
<option value="opt_America">America</option>
</select></td>
<td><span style="color:red; display:none" id="country_Err">Please select your country</span></td>
</tr>
<tr>
<td class="left_Side">Gender :</td>
<td><input type="radio" name="rdio" value="male" /> Male
<input type="radio" name="rdio" value="female" /> Female </td>
<td><span style="color:red; display:none" id="gender_Err">Please select gender</span></td>
</tr>
<tr>
<td class="left_Side">Preferences :</td>
<td colspan="2"><input type="checkbox" value="chk_Red" /> Red
<input type="checkbox" value="chk_Black" /> Black
<input type="checkbox" value="chk_Cyan" /> Cyan
</td>
<td><span style="color:red; display:none" id="Name_Err">Please select preference</span></td>
</tr>
<tr>
<td class="left_Side">Phone :</td>
<td><input type="text" name="phone_number" id="contact" /></td>
<td><span style="color:red; display:none" id="contact_Err">Please enter valid number</span></td>
<td><span style="color:red; display:none" id="con_Err">Only numbers</span> </td>
</tr>
<tr>
<td class="left_Side">Email:</td>
<td><input type="text" name="emil_id" id="check_email" /></td>
<td><span style="color:red; display:none" id="Name_Err">Please enter valid email</span></td>
</tr>
<tr>
<td class="left_Side">Password( 6-8 characters) :</td>
<td><input type="password" name="pssword" id="pasword" /></td>
<td><span style="color:red;display:none" id="password_Err">Enter valid password!</span></td>
</tr>
<tr>
<td class="left_Side">Verify Password :</td>
<td><input type="password" name="verfy_password" id="vry_pasword" /></td>
<td><span style="color:red;display:none" id="Pass_Err">Different from password!</span></td>
</tr>
<tr>
<td><br />
<br />
<br /></td>
<td><input style="margin-left:0px" id="submit_Button" type="submit" value="SEND" />
<input style="margin-left:20px" id="reset_Button" type="reset" value="CLEAR" /></td>
</tr>
</table>
</form>
</body>

</html>

最佳答案

它现在也在 else 中,您将正则表达式与 element 相匹配,实际上您想要的是匹配它的值。

更新

检查复选框是否被选中,给它们相同的类并检查它们是否被选中。

function ValidateForm() {
var err = true;
alert("in submit");
if ($("#name").val() == null || $("#name").val() == "") {
alert("in if");
$("#name_Err").show();
$("#name").focus();
err = false;
} else {
$("#name_Err").hide();
if (!$("#name").val().match(/^[a-zA-Z ]+$/)) {
alert("i m in regu");
$("#name").focus();
err = false;
}
}

if($(".color:checked").length>0){
alert('checkbox is checked do what ever you want to do');

}
else{
alert('check atleast one checkbox');
err = false;
}
return err;
}
 .form {
margin - left: 450 px;
width: auto;
height: 500 px
}
.class = "left_Side" {
text - align: left;
width: 80 px;
position: absolute;
padding - left: 50 px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>


<form name="myForm" onsubmit="return ValidateForm();" action="" class="form">
<table id="form_Table">
<tr>
<h1 style="text-align:Left; margin-left:20px;"> Javascript Validation</h1>
</tr>
<tr>
<td class="left_Side">Name :</td>
<td><input type="text" id="name" /></td>
<td><span style="color:red; display:none" id="name_Err">Please enter your name</span></td>
<td><span style="color:red; display:none" id="na_Err">Only alphabets</span></td>
</tr>
<tr>
<td class="left_Side">Address :</td>
<td><input type="text" id="address" /></td>
<td><span style="color:red; display:none" id="address_Err">Please enter your address</span></td>
<td><span style="color:red; display:none" id="add_Err">Only alphabets</span> </td>
</tr>
<tr>
<td class="left_Side">Zip Code :</td>
<td><input type="text" id="zip_code" /></td>
<td><span style="color:red; display:none" id="zipCode_Err">Please enter your zip code</span></td>
<td><span style="color:red; display:none" id="zip_Err">Only numbers</span> </td>
</tr>
<tr>
<td class="left_Side">Country :</td>
<td><select id="country_Select">
<option value="">Select Country</option>
<option value="opt_India">India</option>
<option value="opt_Australia">Australia</option>
<option value="opt_America">America</option>
</select></td>
<td><span style="color:red; display:none" id="country_Err">Please select your country</span></td>
</tr>
<tr>
<td class="left_Side">Gender :</td>
<td><input type="radio" name="rdio" value="male" /> Male
<input type="radio" name="rdio" value="female" /> Female </td>
<td><span style="color:red; display:none" id="gender_Err">Please select gender</span></td>
</tr>
<tr>
<td class="left_Side">Preferences :</td>
<td colspan="2"><input type="checkbox" value="chk_Red" class="color" /> Red
<input type="checkbox" value="chk_Black" class="color"/> Black
<input type="checkbox" value="chk_Cyan" class="color"/> Cyan
</td>
<td><span style="color:red; display:none" id="Name_Err">Please select preference</span></td>
</tr>
<tr>
<td class="left_Side">Phone :</td>
<td><input type="text" name="phone_number" id="contact" /></td>
<td><span style="color:red; display:none" id="contact_Err">Please enter valid number</span></td>
<td><span style="color:red; display:none" id="con_Err">Only numbers</span> </td>
</tr>
<tr>
<td class="left_Side">Email:</td>
<td><input type="text" name="emil_id" id="check_email" /></td>
<td><span style="color:red; display:none" id="Name_Err">Please enter valid email</span></td>
</tr>
<tr>
<td class="left_Side">Password( 6-8 characters) :</td>
<td><input type="password" name="pssword" id="pasword" /></td>
<td><span style="color:red;display:none" id="password_Err">Enter valid password!</span></td>
</tr>
<tr>
<td class="left_Side">Verify Password :</td>
<td><input type="password" name="verfy_password" id="vry_pasword" /></td>
<td><span style="color:red;display:none" id="Pass_Err">Different from password!</span></td>
</tr>
<tr>
<td><br />
<br />
<br /></td>
<td><input style="margin-left:0px" id="submit_Button" type="submit" value="SEND" />
<input style="margin-left:20px" id="reset_Button" type="reset" value="CLEAR" /></td>
</tr>
</table>
</form>

关于jquery - 使用 jQuery 进行表单验证并具有一些效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com