Javascript name validation

后端 未结 3 1221
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 08:03

I have a javascript function written to validate a field on my form. This function is supposed to make sure the field is not empty, does not exceed the limit of 35 characters an

3条回答
  •  一个人的身影
    2021-01-23 08:33

    Your regular expression is broken. You've escaped the closing square bracket around the character set. Try this:

    else if (/[^a-zA-Z0-9\-]/.test( stringf ))
    

    Also, there's a lot of weird clutter in there that's annoying but not fatal: How many times do you really need to call getElementById('family') in that method? Once.

    if (stringf=="")
    {
        alert("Family name must be filled out");
        return false;
    }
    else if (stringf.length > 35)
    {
        alert("Family name cannot be more than 35 characters");
        return false;
    }
    else if (/[^a-zA-Z0-9\-]/.test( stringf ))
    {
        alert("Family name can only contain alphanumeric characters and hypehns(-)")
        return false;
    }
    return true;
    

提交回复
热议问题