开发者

Reject spaces in javascript form validation

I'm using the following form validation, but I'd like to raise an error if spaces are used in the firstname/lastname/phone fields. Any ideas?

<script type="text/javascript">
function validateFormOnSubmit(theForm) {

var reason = "";

  reason += validateUsername(theForm.first_name);
  reason += validateLastname(theForm.last_name);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);      

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a first name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The first name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = ""; 

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a phone number.\n";
   开发者_Python百科 } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLastname(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a last name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The last name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
</script>


if(name.test(/\s+/g)){
   // there is one or more white spaces in the name
}


Your on the right track using the illegalChars concept. However, it is not consistent on your different validation methods. Also the RegEx will not match like you want. I suggest adding the same logic in validateUsername in all your validation method use these RegEx's:

function validateUsername(val) {
    var illegalChars = /[^a-zA-Z0-9_]/;
    ...
}

function validatePhone(val) {
    var illegalChars = /[^0-9().-]/; // No spaces allowed
    // Or
    var illegalChars = /[^0-9]/; // Only numbers no spaces or dashes or (area code)
    ...
}

Also have you concidered using throw, try, and catch to manage the error?

function test() {
    throw new Error("Illegal Characters");
}

try
{
    test();
}
catch (e)
{
    alert(e); // Displays: "Error: Illegal Characters"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜