0

I'm trying to prevent the form being submitted if the validation fails, however its not working at the moment. The onclick method is used for hidden input, but its the onsubmit method that is failing. Any ideas?

      <input type="submit" class="btn ccm-input-submit" id="register" name="register" value="Register" onsubmit="return validation(this)" onclick="copytextbox()"/>
      <?php echo $form->hidden('rcID', $rcID); ?>

<script>
function copytextbox()
{
    var textToCopy = document.getElementById('school').value;
    var whereToCopy = document.getElementById('<?php echo $school ?>');
    whereToCopy.value += textToCopy; 
}

function validation(f)
{
    var schoolText = document.getElementById('school').value;
    if (schoolText == '') { 
      alert('Please Choose a value from the dropdown box.');  
      return false;  // stop submission until textbox is not '' 
    }  else {
      f.submit();
      return false;
    }
}
</script>

Also, im attempting to see if that value is a member of an array, I tried using indexOf(schoolText) etc but this also failed, kind regards

2
  • for front walidation use jquery.validate, for php validation use Respect/Validation Commented Mar 13, 2015 at 14:43
  • remember that a hacker can bypass the in-browser validation easily. Commented Mar 13, 2015 at 18:48

1 Answer 1

3

Move your onsubmit="return validation(this)" attribute from the <input> tag to the <form> tag.

Test an array like this:

// global array
var fruits = ["Banana", "Orange", "Apple", "Mango"];

function findFruit(val) {
  if(fruits.indexOf(val) == -1 {
    alert('not found');
  } else {
    alert('Found!')
  }
}
findFruit('Peach'); // not found
Sign up to request clarification or add additional context in comments.

2 Comments

Short and sweet thanks, any ideas on how to check if the value is part of an array?
Updated my answer for the Array question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.