Jump to content

JS Form validator ?


megz90

Recommended Posts

hi i think ive missed a { or a } somewhere my JS will let the dater pass as valid when it is not.

ive got this form with 12 fields in it.

9 of 12 are required

2 of 12 is numbers only

1 of 12 is email

 

this is what ive done so far. check for the required fields not being left blank

(havent got to the check for numbers only or letters only yet (pointers would be handy))

when checking the email address it seems to be ok and lets the data through as valid.

 

<script language="javascript">
invalid = "/:,;"
function validateForm(form)
{
if (form.dbOwnerId.value == "" || form.dbFname.value == ""
|| form.dbLname.value == "" || form.dbemail.value == "" 
|| form.dbmainphone.value == "" || form.dbaddress.value == ""
|| form.dbcity.value == "" || form.dbpostcode.value == ""
|| form.dbpassword.value == "")
{
alert ("Missing compulsory fields")
form.dbFname.focus()
return false
  }
dbemail = form.dbemail.value
howmany = dbemail.length
alert ("length is "+ howmany)
if (dbemail != "") {
     for (i=0; i<invalidchars.length; i++) {
        badchar = invalidchars.charAt(i)
        if (dbemail.indexOf(badchar, 0) > -1) {
           alert ("Invalid emaillll")
           form.dbemail.focus()
           return false
        } 
     }
 }

     if (dbemail.indexOf("@", 0) == -1) {
        alert ("Invalid m_email m_add")
        form.m_email.focus()
        return false
     }

{
    return true
  }
  }
</script>

this is the data from my form

<form method="post" onsubmit="return validateForm(this)" action="register.php">

the input names are as below
name="dbOwnerId"	required	any
name="dbFname"		required	letters
name="dbLname"		required	letters
name="dbemail"		required	email
name="dbmainphone" 	required	num
name="dbotherphone"			num
name="dbaddress"	required	any
name="dbaddress1" 			any
name="dbcity"		required	letters
name="dbpostcode"	required	mixed max 9
name="dbnotes" 				any
name="dbpassword"	required	any

 

can anyone see if im missing something?

Link to comment
Share on other sites

Yeah, there are some problems with the opening and closing brackets. Your opening script tag should be using "type" instead of "language". Here are some validations that should work for you below:

 

The alpha check allows letters, the space, the hyphen and the apostrophe (since names can contain those characters).

 

The phone check allows numbers, parenthesis, space, and the hyphen

 

The email check only allows properly formatted email addresses.

 

 

You can get more restrictive on these validations (e.g. phone numbers must be 7 or 10 characters), but I included validations based upon your post.

 

<script type="text/javascript">

function validateForm(form)
{
  var fe = form.elements;

  //validate owner id
    if (!requiredCheck(fe['dbOwnerId'], 'Owner')) return false;
  //validate first name
    if (!requiredCheck(fe['dbFname'], 'First Name')) return false;
    if (!validAlpha(fe['dbFname'], 'First Name')) return false;
  //validate last name
    if (!requiredCheck(fe['dbLname'], 'Last Name')) return false;
    if (!validAlpha(fe['dbLname'], 'Last Name')) return false;
  //validate email address
    if (!requiredCheck(fe['dbemail'], 'Email address')) return false;
    if (!validEmail(fe['dbemail'], 'Email address')) return false;
  //validate main phone
    if (!requiredCheck(fe['dbmainphone'], 'Main Phone')) return false;
    if (!validPhone(fe['dbmainphone'], 'Main Phone')) return false;
  //validate other phone
    if (!validPhone(fe['dbotherphone'], 'Other Phone')) return false;
  //validate address
    if (!requiredCheck(fe['dbaddress'], 'Address')) return false;
  //validate address 1
    if (!requiredCheck(fe['dbcity'], 'City')) return false;
    if (!validAlpha(fe['dbcity'], 'City')) return false;
  //validate post code
    if (!requiredCheck(fe['dbpostcode'], 'Post Code')) return false;
  //validate password
    if (!requiredCheck(fe['dbpassword'], 'Password')) return false;

  return true;
}

function requiredCheck(fieldObj, fieldName) {
    if (!fieldObj.value) {
        alert(fieldName+' is required.');
        fieldObj.focus();
        return false;
    }
    return true;
}

function validEmail(fieldObj, fieldName) {
    validEmailRegEx = /^[\w\+-]+([\.]?[\w\+-])*@[a-zA-Z0-9]{2,}([\.-]?[a-zA-Z0-9]{2,})*\.[a-zA-Z]{2,4}$/
    if (!fieldObj.value.match(validEmailRegEx)) {
        alert(fieldName+' is not in the proper format.');
        fieldObj.focus();
        return false;
    }
    return true;
}

function validAlpha(fieldObj, fieldName) {
    validAlphaRegEx = /^[a-zA-Z '-]*$/
    if (!fieldObj.value.match(validAlphaRegEx)) {
        alert(fieldName+' contains invalid characters.');
        fieldObj.focus();
        return false;
    }
    return true;
}

function validPhone(fieldObj, fieldName) {
    validNumberRegEx = /^[\d \-()]*$/
    if (!fieldObj.value.match(validNumberRegEx)) {
        alert(fieldName+' contains invalid characters.');
        fieldObj.focus();
        return false;
    }
    return true;
}


</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.