简体   繁体   English

jQuery简单电子邮件验证

[英]Jquery simple email validation

started learning jquery/js/html/css a week ago. 一周前开始学习jquery / js / html / css。 We have to create a basic one page website, one feature is validation. 我们必须创建一个基本的一页网站,其中一个功能是验证。 Since we arent allowed to use other languages or any form of storage, i have a simple form which doesnt store the data, simply needs to validate it. 由于我们不允许使用其他语言或任何形式的存储,因此我有一个不存储数据的简单形式,只需要对其进行验证即可。

<form id="frm-contact">
    Full name: <input type="text" name="contact-name"> <br>
    Email Address: <input type="text" name="contact-email" > <br>
    Mobile No:<input type="tel" name="contact-tel"> </br>
    <button type="submit" id="btn-submit" class="btn">Submit</button>
    </form>

 $("#btn-submit").click() { var text = $('input[type="text"][name=contact-email]').val(); var emailReg = /^([\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4})?$/; //check input against regex - if passes - alert saying "well be in touch soon" //else "please insert valid email //repeat until valid } 
 <form id="frm-contact"> Full name: <input type="text" name="contact-name"> <br>Email Address: <input type="text" name="contact-email"> <br>Mobile No: <input type="tel" name="contact-tel"> </br> <button type="submit" id="btn-submit" class="btn">Submit</button> </form> 

jsfiddle response would be much appreciated!! jsfiddle的响应将不胜感激! First post on here so thank you to anyone who answers 在这里的第一篇文章,所以谢谢所有回答的人

There are few mistakes in your code .Syntax error in your click function 您的代码中几乎没有错误。click函数中的语法错误

Your regular expression for email validation is not valid too 您用于电子邮件验证的正则表达式也无效

check the following snippet 检查以下代码段

 $(document).ready(function() { $("#btn-submit").click(function(e) { e.preventDefault(); var text = $('input[name=contact-email]').val(); var re = "/^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/"; //check input against regex - if passes - alert saying "well be in touch soon" //else "please insert valid email //repeat until valid if (text.match(re)) alert("will be in touch soon"); else alert("invalid email") }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <form id="frm-contact"> Full name: <input type="text" name="contact-name"> <br>Email Address: <input type="text" name="contact-email"> <br>Mobile No: <input type="tel" name="contact-tel"> <button type="submit" id="btn-submit" class="btn">Submit</button> </form> 

Hope this helps 希望这可以帮助

https://jsfiddle.net/ibrahimince/w9m34afa/

You can have a loook this lovely form submit that I have made ready for you:) 您可以瞧瞧我为您准备的这份可爱的表格:)

function isUsernamePresent() { return $username.val().length > 0; }


function isPasswordValid() {
  return $password.val().length > 2;
}

function arePasswordsMatching() {
  return $password.val() === $confirmPassword.val();
}

function canSubmit() {
  return isPasswordValid() && arePasswordsMatching() && isUsernamePresent();
}

function passwordEvent(){
    //Find out if password is valid  
    if(isPasswordValid()) {
      //Hide hint if valid
      $password.next().hide();
    } else {
      //else show hint
      $password.next().show();
    }
}

function confirmPasswordEvent() {
  //Find out if password and confirmation match
  if(arePasswordsMatching()) {
    //Hide hint if match
    $confirmPassword.next().hide();
  } else {
    //else show hint 
    $confirmPassword.next().show();
  }
}

Don't re-invent client-side form validation. 不要重新发明客户端表单验证。 There are standards for this. 有为此的标准。 In fact, e-mail is built-in. 实际上,电子邮件是内置的。

Then, you can use checkValidity() . 然后,您可以使用checkValidity() https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms_in_HTML#Constraint_Validation https://developer.mozilla.org/zh-CN/docs/Web/Guide/HTML/Forms_in_HTML#Constraint_Validation

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

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