Jump to content

validate form... not working.


megz90

Recommended Posts

hi  this JS was created using a previous post. i cant seem to get it working.

form allows empty and incorrect values through to the next page.

 

<script type="text/javascript">
function validateForm(form)
{
  var fe = form.elements;

  //validate horse
    if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) return false;
  //validate dob
    if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) return false;
    if (!validDob(fe['dbDOB'], 'Date of Birth')) return false;
  // ueln
    if (!requiredCheck(fe['dbUELN'], 'UELN')) return false;
    if (!validPhone(fe['dbUELN'], 'UELN')) return false;

  return true;
}

function requiredCheck(fieldObj, fieldName) {
    if (!fieldObj.value) {
        alert(fieldName+' is required. Please fill in missing field.');
        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 validDob(fieldObj, fieldName) {
    validDobRegEx = (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}
    if (!fieldObj.value.match(validDateRegEx)) {
        alert(fieldName+' is  not a valid date. Please correct and try again.');
        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>

 

start of the form

echo "     <form method=\"post\" onsubmit=\"return validateForm(this)\" action=\"updatehorse.php\"> ";

 

there are 7 elements in the form, only 3 need to be checked.

dbhorsename required any

dbDOB date req dd /- mm /- yy

dbUELN numbers only

 

im not sure whats going on with it but at the mo anything goes through...

thanks

Link to comment
Share on other sites

one thing I have had small problems with using functions that return true or false is the leaking of information, you final statement in the function says return true so it will, most of the time try this

function validateForm(form)
{
  var fe = form.elements;
  var answer = true;
  //validate horse
    if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) answer = false;
  //validate dob
    if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) answer =  false;
    if (!validDob(fe['dbDOB'], 'Date of Birth')) answer =  false;
  // ueln
    if (!requiredCheck(fe['dbUELN'], 'UELN')) answer =  false;
    if (!validPhone(fe['dbUELN'], 'UELN')) answer = false;

  return answer;
}

 

so it sets up a variable with the value of "true" and changes it if there are any problems and then returns that variable instead of the global true or false

Link to comment
Share on other sites

thanks for the reply but the code you provided with the top part of mine still did the same thing...

 

is there something that im missing ? the previous code...

http://www.phpfreaks.com/forums/index.php/topic,186425.msg835327.html#msg835327

worked fine and when i edited it to work on my other forms it also was alright.

it is just on this form im getting problems.

 

 

<script type="text/javascript">
function validateForm(form)
{
  var fe = form.elements;
  var answer = true;
  //validate horse
    if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) answer = false;
  //validate dob
    if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) answer =  false;
    if (!validDob(fe['dbDOB'], 'Date of Birth')) answer =  false;
  // ueln
    if (!requiredCheck(fe['dbUELN'], 'UELN')) answer =  false;
    if (!validPhone(fe['dbUELN'], 'UELN')) answer = false;

  return answer;
}

function requiredCheck(fieldObj, fieldName) {
    if (!fieldObj.value) {
        alert(fieldName+' is required. Please fill in missing field.');
        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 validDob(fieldObj, fieldName) {
    validDobRegEx = (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}
    if (!fieldObj.value.match(validDateRegEx)) {
        alert(fieldName+' is  not a valid date. Please correct and try again.');
        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.