初学者 javascript onblur 验证未验证
我刚刚学习 javascript 和 jquery,我的验证一定是错误的。我尝试了几种不同的方法,每次第一个警报 (test1) 都会触发 onblur,但一封错误的电子邮件不会引发第二个警报。我考虑过使用 jquery 验证插件,但是在玩了一段时间后,我意识到我需要对每个空白 onblur 进行验证,而不是在处理表单时进行验证,所以我认为我被困在普通的 js 中。
在我的文档准备功能中:
$("#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");
}
}
});
在我的 HTML 中:
<input type="text" class="signUpTextbox" id="studentEmail" name="registerStudentEmail">
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.
Or, if this function has zero reuse you could just do this:
为什么这里有一个内部函数?这应该有效
Why do you have an inner function here ? This should work