简体   繁体   English

Javascript:验证警报2

[英]Javascript: Validation alerts 2

My goal is this: 我的目标是这样的:

  1. Check if email and name are empty. 检查emailname是否为空。 If so, give 'Enter email or name' alert. 如果是这样,请给“输入电子邮件或姓名”提示。
  2. If they do, check for an @ in email If none is found, give 'Bad email' alert. 如果是这样,请检查email是否有@。如果找不到,请给“错误的电子邮件”警报。
  3. Check if email and name contain any letters, if they do, give 'Success' alert 检查emailname包含任何字母,如果包含,则发出“成功”警报

     function test(email, name){ if(email=="" || name == "") { alert("Enter mail or name");} return false; if(email.indexOf("@") == -1){ alert("Bad email");} return false; var a = email.length; var b = name.length; if(a==>0, b==>0){ alert("Message sent");} return true; } 

This is what I've come up with so far, but it isn't working. 到目前为止,这是我想出的,但是没有用。 I'm quite new at javascript so maybe you guys could tell me what I've done wrong? 我在javascript上还很新,所以也许你们可以告诉我我做错了什么?

The problem you're having is the close bracket is in the wrong place. 您遇到的问题是右括号放在错误的位置。 You have it at the end of your alert statement and you probably want the return to be included with your if statement. 您将其放在警报语句的末尾,并且可能希望将返回值包含在if语句中。 if this is the case then change it to be: 如果是这种情况,则将其更改为:

 function test(email, name){

        if(email=="" || name == "") {
            alert("Enter mail or name");
            return false;
        }

        if(email.indexOf("@") == -1){
            alert("Bad email");
            return false;
        }

        var a = email.length;
        var b = name.length;

        if(a > 0 && b > 0){
            alert("Message sent");
            return true;
        }
}

A better way to do the same thing would be because that way you're not checking the variables for length and size twice: 做同一件事的更好的方法是,因为这样您就不必两次检查变量的长度和大小了:

function test(email, name) {
   var a = email.length;
   var b = name.length;

   if ( a > 0  && b > 0 ) {
      // ignore 0 because email addresses shouldn't start with @
      if ( email.indexOf("@") > 0 ) {
         alert("Message sent");
         return true;
      }
      else {
        alert("Bad email");
        return false;
      }
   }
   else {
      alert("Enter mail or name");
      return false;
    }
}

Try this JSFiddle that seems to fit your needs http://jsfiddle.net/9nF5W/ 试试看这个符合您需求的JSFiddle http://jsfiddle.net/9nF5W/

function test(email, name) {

    if (email == "" || name == "") {
        alert("Enter mail or name");
        return false;
    }

    if (email.indexOf("@") == -1) {
        alert("Bad email");
        return false;
    }

    var a = email.length;
    var b = name.length;

    if (a > 0 && b > 0) {
        alert("Message sent");
    }
    return true;
}

test('tes@t', 'test');

I think there is an other mistake than the returns statements in " if(a==>0, b==>0){ " by the way. 我认为顺便说一下if(a==>0, b==>0){除了“ if(a==>0, b==>0){ ”中的return语句之外,还有另一个错误。

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

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