var Surname1 = $('#sn1').val();
var Name1 = $('#n2').val();
var Gender1 = $('#g3').val();
var Address1 = $('#a4').val();
var Email1 = $('#em5').val();
var Phone1 = $('#hp6').val();
var emailcheck = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var phonecheck = /^\d{8}$/;
var gendermcheck = /^[Male]$/;
var genderfcheck = /^[Female]$/;
if (Surname1 == "" || Name1 == "" || Gender1 == "" || Address1 == "" || Email1 == "" || Phone1 == "")
{
alert("Updating failed! Please try again!!!");
return false;
}
else if (!emailcheck.test(Email1))
{
alert("Unable to Update! Please Check your Email Address");
return emailcheck.test(Email1);
}
else if (!phonecheck.test(Phone1))
{
alert("Unable to Update! Please Check your contact number");
return phonecheck.test(Phone1);
}
else if (!genderfcheck.test(Gender1) || gendermcheck.test(Gender1))
{
alert("Type Male/Female");
return genderfcheck.test(Gender1);
return gendermcheck.test(Gender1);
}
else
{
alert("Update was Successful! Thank you for updating our system!");
}
验证男性和女性如果我想确保用户只能在框中输入男性或女性来保存。如果他们输入男性,它会提示错误是否有任何方法让我编码,以便它只能接受“男性”或“女性”?
答案 0 :(得分:0)
正如其他人所说,如果你有一个预定义的选项列表,你应该使用单选按钮或下拉列表。
但要回答你的问题,如果你想让用户只输入男性或女性(区分大小写),你应该改变他们相应的正则表达式,如下所示:
var gendermcheck = /^Male$/;
var genderfcheck = /^Female$/;
答案 1 :(得分:0)
您需要更改else if子句的逻辑:
else if (!genderfcheck.test(Gender1) && !gendermcheck.test(Gender1))
{
alert("Type Male/Female");
return genderfcheck.test(Gender1);
return gendermcheck.test(Gender1);
}
另外你必须改变你的正则表达式: " / ^ [男性] $ /"仅匹配括号中的单个字符。 (" M"," l",......)。使用" / ^男$ /"代替。女性也一样。
提示:我会使用Listbox控件。这看起来有点像这样:
<form action="select.html"> <label>Gender: <select name="gender" > <option>Male</option> <option>Female</option> <option>Other...</option> </select> </label> </form>