我想确保用户只输入一个名称(名字或姓氏,但不是两者都没有),同时确保输入的名称长于3个字符。
我已经尝试过这样做,但是将它留空并使它更多的时候会发生碰撞。
这是我的代码:
if ($("#firstName").val().length > 0 && $("#lastName").val().length > 0) {
event.preventDefault();
$("#error").slideDown().text("You may enter just first name or last name");
return;
} else if ($("#firstName").val().length <= 2 || $("#lastName").val().length <= 2) {
event.preventDefault();
$("#error").slideDown().text("Your name have to contain at least 3 letters");
return;
}
可以找到小提琴here
答案 0 :(得分:1)
检查名称中是否有空格。如果存在,则用户输入全名
试试这个:
var name = $.trim($('#firstName').val());
if (name.length < 3 && name.indexOf(' ') > -1) {
alert("Invalid Name; Enter first name or last name only");
}
答案 1 :(得分:1)
嗨你应该稍微改变一下你的状况。它有点冗长,但却有用。
if ($("#firstName").val().length > 0 && $("#lastName").val().length > 0) {
event.preventDefault();
$("#error").slideDown().text("You may enter just first name or last name");
return;
}
if($("#firstName").val().length > 0){
if ($("#firstName").val().length <= 2){
event.preventDefault();
$("#error").slideDown().text("Your first name have to contain at least 3 letters");
return;
}
}
if($("#lastName").val().length > 0){
if ($("#lastName").val().length <= 2){
event.preventDefault();
$("#error").slideDown().text("Your last name have to contain at least 3 letters");
return;
}
}
答案 2 :(得分:0)
if ($("#firstName").val().length > 0 && $("#lastName").val().length > 0) {
event.preventDefault();
$("#error").slideDown().text("You may enter just first name or last name");
return;
} else if ($("#firstName").val().length <= 2 || $("#lastName").val().length <= 2) {
event.preventDefault();
$("#error").slideDown().text("Your name have to contain at least 3 letters");
return;
}
您检查输入的两个字段。然后检查两个字段是否都是&lt; = 2。 即使只输入一个名称而另一个名称为空,后者也是如此。 你应该像这样检查
if ($("#firstName").val().length > 0 && $("#lastName").val().length > 0) {
event.preventDefault();
$("#error").slideDown().text("You may enter just first name or last name");
return;
} else if (!($("#firstName").val().length > 2 || $("#lastName").val().length > 2)) {
event.preventDefault();
$("#error").slideDown().text("Your name have to contain at least 3 letters");
return;
}
答案 3 :(得分:0)
请看这个fiddle 我希望这正是你要找的,
$("form").submit(function (event) {
// TODO 1: Make sure age has only digits
// TODO 2: Make sure names do not have digits
// TODO 3: Make the email is valid (has @ and . inside of them - do not use RegEx, use indexOf)
// TODO 4: Make sure each name is longer than 2 chars if the input is not empty
if (!$("#firstName").val() && !$("#lastName").val()) {
event.preventDefault();
$("#error").slideDown().text("You may enter first name or last name");
return;
}else if ($.isNumeric($('#firstName').val()) || $.isNumeric($('#lastName').val())) {
event.preventDefault();
$("#error").slideDown().text("Your name can contain letters only");
return;
}else if (!($("#firstName").val().length > 2 || $("#lastName").val().length > 2)) {
event.preventDefault();
$("#error").slideDown().text("Your name have to contain at least 3 letters");
return;
}
if ($("#email").val().indexOf("@") < 0 || $("#email").val().lastIndexOf(".") < 0) {
event.preventDefault();
$("#error").slideDown();
$("#error").text("Your Email is not valid.");
return;
}
if ($("#age").val() == "" || $("#age").val() < 18) {
event.preventDefault();
$("#error").slideDown().text("You must be over 18 to register");
return;
}
if (!$.isNumeric($('#age').val())) {
event.preventDefault();
$("#error").slideDown().text("Your age can contain numbers only");
return;
}
if (!$("#terms").is(":checked")) {
event.preventDefault();
$("#error").slideDown().text("Please accept terms");
return;
}
$("#error").slideDown().text("we can now submit the form");
// TODO 5: Make sure the user inputed excacly one name (first name, or last name, but not both or non)
});