简体   繁体   English

Javascript:验证警报

[英]Javascript: Validation alerts

what i need to do is the following: 我需要做的是以下几点:

  1. Alert 'empty field' if name OR email is empty 如果nameemail为空,则提醒“空字段”
  2. Alert 'bad email' if email does not contain a @ 如果email中不包含@,则提醒“不良电子邮件”
  3. Alert 'success' if both name and email is filled in correctly 如果正确填写nameemail则提示“成功”

     function test10(email, name){ if(email=="") { alert("empty field");} else if(email.indexOf("@")<0){ alert("bad email");} if(name=="") { alert("empty field");} } 

This is what i've come up with so far, i want to keep it as simple as possible. 到目前为止,这是我想出的,我想使其尽可能简单。 Now how would i in this same vein make it say "success" when both are filled in? 现在,当我两个都填写时,我又该如何表达“成功”? I need to do it purely in javascript. 我只需要用javascript做就可以了。

It's probably worth mentioning that i'm very new to javascript so keep the insults to yourselves if you think this is stupid, i'm just trying to learn here. 可能值得一提的是,我对javascript还是很陌生,所以如果您认为这很愚蠢,请尽量避免侮辱自己,我只是想在这里学习。

Try this: 尝试这个:

function test10(email, name){
    // The || is a boolean "OR" , so if (email equals "") OR (name equals "")
    //      then alert the failure reason.
    if(email=="" || name == "") {
        alert("empty field");
        return false;
    }
    if(email.indexOf("@") == -1){
        alert("bad email");
        return false;
    }
    //Conditions are met.
    alert("Succes!");
    return true;
}

This works exactly as you specified. 这完全按照您指定的方式工作。

Either you put a final else clause at the end of your function and alert the success message from there, or you could return from the function whenever you hit an error, making sure to stop the function from continue running when it run into an invalid input. 您可以在函数的末尾放置一个final else子句并从那里警告成功消息,或者在遇到错误时可以从函数返回,请确保在遇到无效输入时阻止该函数继续运行。 You could then have an alert at the end of the function that says success, since you will never get to that point as longs as you still have errors in the input. 然后,您可能会在函数结束时收到一条警告,说成功,因为只要输入中仍然有错误,您就永远无法达到目标。

Something like this: 像这样:

function test10(email, name){

    if (email === "" || name === "") {
        alert("empty field");
        return false;
    }
    if(email.indexOf("@")<0){
        alert("bad email");
        return false;
    }

    // If we get here, the input is all good!
    alert("success");
    return true;
}

Like this you will only alert the user about one error at a time, giving her time to fix that error, instead of throwing up three alerts after each other, if non of your rules are met. 这样,您只会一次警告用户有关一个错误的信息,让她有时间来解决该错误,而不是在未满足您的规则的情况下互相抛出三个警告。

Returning false/true from the function has the benefit that the function then can be used to stop a form submission if the input is invalid. 从函数返回false / true的好处是,如果输入无效,则可以使用该函数停止表单提交。

Alternative validation method 替代验证方法

With HTML5, data form validation was introduced, providing a native option for validation using attributes like required and pattern . 借助HTML5,引入了数据表单验证,从而提供了使用诸如requiredpattern类的属性进行验证的本机选项。 Browsers can also perform validation on email input if you use the new <input type="email"> 如果您使用新的<input type="email">浏览器还可以对电子邮件输入执行验证

More extensive reading on this is available at MDN. MDN上对此有更广泛的阅读

Try this 尝试这个

   function test10(email, name){

if(email=="" || name=="") {
    alert("empty field");}

else if(email.indexOf("@")<0){
    alert("bad email");}
else{
alert("success");}

}

Just add a return statement in each of the wrong cases 只需在每种错误情况下添加return语句

function test10(email, name)
{

    if(email=="") {
        alert("empty field");
        return false;
    }

    if(email.indexOf("@")<0){
        alert("bad email");
        return false;

    }

    if(name=="") {
       alert("empty field");
       return false;
    }

    alert("success");
    return true;

 }

Note: you need to add in the form event onsubmit="return test10(email,name);" 注意:您需要在表单事件中添加onsubmit="return test10(email,name);"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM