jquery - Beginner javascript onblur validation not validating - Stack Overflow

I am just learning javascript and jquery, and I must be doing my validation wrong. I have tried a few d

I am just learning javascript and jquery, and I must be doing my validation wrong. I have tried a few different ways, and each time, the first alert (test1) fires onblur, but a bad email does not bring up the second alert. I thought about using jquery validation plugin, but after playing with in for a while, I realized that I need validation on each blank onblur, not when it is time to process the form, so I think I am stuck with normal js.

In my document ready function:

$("#studentEmail").blur(function() {
    alert ("test1");
    function validateEmail(studentEmail){  
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
        if(!emailReg.test(studentEmail)) {
            alert("Please enter valid email id");
        }
    }
});

In my HTML:

<input type="text" class="signUpTextbox" id="studentEmail" name="registerStudentEmail">

Thanks!

I am just learning javascript and jquery, and I must be doing my validation wrong. I have tried a few different ways, and each time, the first alert (test1) fires onblur, but a bad email does not bring up the second alert. I thought about using jquery validation plugin, but after playing with in for a while, I realized that I need validation on each blank onblur, not when it is time to process the form, so I think I am stuck with normal js.

In my document ready function:

$("#studentEmail").blur(function() {
    alert ("test1");
    function validateEmail(studentEmail){  
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
        if(!emailReg.test(studentEmail)) {
            alert("Please enter valid email id");
        }
    }
});

In my HTML:

<input type="text" class="signUpTextbox" id="studentEmail" name="registerStudentEmail">

Thanks!

Share Improve this question edited Dec 15, 2011 at 15:27 Nick Craver 631k138 gold badges1.3k silver badges1.2k bronze badges asked Dec 15, 2011 at 15:10 radleybobinsradleybobins 9795 gold badges10 silver badges24 bronze badges 1
  • Firebug (FF extension) can help you debug problems. Once you install it and bring it up you can open the "Script" tab and set breakpoints, etc. – Jeroen Commented Dec 15, 2011 at 15:17
Add a ment  | 

3 Answers 3

Reset to default 3

Inside of your blur handler you were declaring a function. Declare your function outside of this handler, and call it inside of your handler.

 function validateEmail(studentEmail){  
     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
     if(!emailReg.test(studentEmail)) {  
         alert("Please enter valid email id");
     }       
 }

 $("#studentEmail").blur(function() { 
    alert ("test1");

    validateEmail($(this).val());
});

Or, if this function has zero reuse you could just do this:

 $("#studentEmail").blur(function() { 
    alert ("test1");

    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
    if(!emailReg.test($(this).val())) {  
        alert("Please enter valid email id");
    }        
});

Why do you have an inner function here ? This should work

$("#studentEmail").blur(function() { 
    alert ("test1");

    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
    if(!emailReg.test(studentEmail)) {  
        alert("Please enter valid email id");
    }       

});
$("#studentEmail").blur(function() { 
    alert ("test1"); 
    function validateEmail(studentEmail){
        var emailReg = /^([\w-.]+@([\w-]+.)+[\w-]{2,4})?$/;
        if(!emailReg.test(studentEmail)) {
            alert("Please enter valid email id");
        }
    }
    // call the validate method
    validateEmail(this.value);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745431507a4627411.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信