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
3 Answers
Reset to default 3Inside 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条)