我在使用 javascript 验证某些字段时遇到了一些问题。我正在尝试测试一个名为 name of 30 characters max 的字段,John Smith、Paul M'Dougall Laura-Jane 都应该测试为真,但 gazz8932 应该测试为假。
EAID 最多应为 16 个字符 123kjg-34_dfs 应测试为真,但 *&$ 等应测试为假。
最后,网址应该来自 youtube.com 或 ea.com,但我不能打赌其中任何一个都可以工作。
如果可以提供任何光,将不胜感激......
// create the errors array
var errors = false;
// clean last errors
$('#goalsubmission p.error').remove();
$('#goalsubmission .error').removeClass('error');
// test the name
var reg = /^[a-z ,.'-]{0,30}/i;
var name = $('#goalsubmission input#name');
if(reg.test(name.val()) == false){
name.parent().addClass('error');
errors == true;
name.parent().append('<p class="error">Make sure you have put your name using only letters and spaces.</p>');
} else {
name.parent().removeClass('error');
}
/*
// test the email
var reg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
var email = $('#goalsubmission input#email');
var result = reg.test(email.val());
if(email.val().length == 0 || result == false){
email.parent().addClass('error');
errors == true;
email.parent().append('<p class="error">Make sure you have entered a valid email address.</p>');
} else {
email.parent().removeClass('error');
}
*/
// test the ea id
var reg = /^[a-z0-9-_]{0,16}/i;
var eaid = $('#goalsubmission input#eaid');
if(reg.test(eaid.val()) == false){
eaid.parent().addClass('error');
errors == true;
eaid.parent().append('<p class="error">Make sure you have entered a valid EAID.</p>');
} else {
eaid.parent().removeClass('error');
}
// test the url
var regYT = /^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$/
var regEA = /^http:\/\/(?:www\.)?ea.com\/?$/
var url = $('#goalsubmission input#url');
if(
// url.val().length == 0 ||
// url.val().length > 255 ||
(regYT.test(url.val()) == false && regEA.test(url.val()) == false)
){
url.parent().addClass('error');
errors == true;
url.parent().append('<p class="error">Make sure you have entered a valid Url, from either YouTube or EA.</p>');
} else {
url.parent().removeClass('error');
}