java 脚本中用户名和电子邮件的表单验证
Form validation for username and Email in java script
我正在尝试验证我的姓名元素和电子邮件地址元素中的值。
但是下面的代码没有提供正确的验证,我的代码仍然跳转到我的 php 文件,其中有错误的输入。
有人可以给我一些指导吗?
var letters = /^[A-Za-z]+$/;
var mailformat = ^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$;
if( document.addform.First_name.value.match(letters)) {
return true;
}
else {
alert( "Username must have alphabet characters only" );
document.addform.First_name.focus() ;
return false;
}
if( document.addform.email.value.match(mailformat) ) {
return true;
}
else {
alert( "You have entered an invalid email address!" );
document.addform.email.focus() ;
return false;
}
如果两个条件都满足,您需要 return 为真,而不仅仅是其中一个。
function validateFormInput () {
let yourUsernameRegEx = new RegExp(...);
let yourMailRegEx = new RegExp(...);
if (!document.addform.First_name.value.match(yourUsernameRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
if (!document.addform.email.value.match(yourMailRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
return true;
}
我正在尝试验证我的姓名元素和电子邮件地址元素中的值。 但是下面的代码没有提供正确的验证,我的代码仍然跳转到我的 php 文件,其中有错误的输入。 有人可以给我一些指导吗?
var letters = /^[A-Za-z]+$/;
var mailformat = ^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$;
if( document.addform.First_name.value.match(letters)) {
return true;
}
else {
alert( "Username must have alphabet characters only" );
document.addform.First_name.focus() ;
return false;
}
if( document.addform.email.value.match(mailformat) ) {
return true;
}
else {
alert( "You have entered an invalid email address!" );
document.addform.email.focus() ;
return false;
}
如果两个条件都满足,您需要 return 为真,而不仅仅是其中一个。
function validateFormInput () {
let yourUsernameRegEx = new RegExp(...);
let yourMailRegEx = new RegExp(...);
if (!document.addform.First_name.value.match(yourUsernameRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
if (!document.addform.email.value.match(yourMailRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
return true;
}