Skip to content Skip to sidebar Skip to footer

Javascript Name Validation

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 and o

Solution 1:

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

elseif (/[^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");
    returnfalse;
}
elseif (stringf.length > 35)
{
    alert("Family name cannot be more than 35 characters");
    returnfalse;
}
elseif (/[^a-zA-Z0-9\-]/.test( stringf ))
{
    alert("Family name can only contain alphanumeric characters and hypehns(-)")
    returnfalse;
}
returntrue;

Solution 2:

UPDATED:

Sorry, the problem is with your regex, i missed that, change to this its fully working:

var ck_password =  /^[A-Za-z0-9-]/;

            if(!ck_password.test(stringf))
            {

                alert("Family name can only contain alphanumeric characters and hypehns(-)")

            }

Console in chrome, go to the OPTIONS in the right top coner, select TOOLS, then DEVELOPER TOOLS.

Solution 3:

Try to rename submit button, rid of id and name "submit" (rename to "doSubmit" or smth), it can cause problems and conflict with event "submit" of form.

Post a Comment for "Javascript Name Validation"