简体   繁体   English

javascript验证电子邮件地址未响应

[英]javascript validation email address not respond

juse have a quick question, i have try to do the validation for email address. juse有一个简短的问题,我尝试对电子邮件地址进行验证。 i need to do a validation such as validate if an email address is a school email(which means end with edu), but i decide to start from validate the normal email, the code below is what i have down. 我需要进行验证,例如验证电子邮件地址是否为学校电子邮件(这意味着以edu结尾),但是我决定从验证普通电子邮件开始,下面的代码是我无法理解的。

Javascript part Javascript部分

function ok_Email(email){
    var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if(filter.test(email)){
        return true;
        window.alert('This is an email')
    }
else{
    return false;
    window.alert('This is not an email')
}
}

Html part HTML部分

<form name="myform"
onSubmit="return ok_Email(this);">
<p>
Please enter your email address: <br>
<input type="text" size=40 name="user_email">
<p>
<input type = submit value="Send">
</form>

The problem for this code is when i click the send buttom, the page did not change. 此代码的问题是,当我单击发送按钮时,页面没有更改。 As you see in the code, it should have an alert comes out but it doesn't. 如您在代码中所见,它应该发出警报,但没有发出。 I think the problem comes from the buttom part but i am not sure....... 我认为问题出在臀部部分,但我不确定.......

The alerts do not appear because the returns are before the alert! 警报不出现,因为返回值早于警报! The code exits at that point and nothing after it will execute. 该代码将在此时退出,之后将不执行任何操作。

Second issue is you are testing the regular expression against an object. 第二个问题是您正在针对对象测试正则表达式。

"return ok_Email(this);">
                 ^^^^
                  this is the form

 function ok_Email(email){
                   ^^^^^
                   You think it is a string

You need to be referencing the value of user_email. 您需要引用user_email的值。

function ok_Email(form){
    var email = form.user_email.value;

you need to change your code to this : 您需要将代码更改为此:

function ok_Email(form.user_email.value){
    var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if(filter.test(foam.user_email.value)){
        window.alert('This is an email');
        return true;
    }
    else{
      window.alert('This is not an email');
      return false;
    }
}

to see the alert boxes and also to pass the value of the Email field 查看警报框并传递“电子邮件”字段的值

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

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