0
votes

The last HTML5 input field before a form's submit button is a spam filter asking the user to solve a simple algebraic equation that resolves to 4 before submitting the form to a separate page for php processing. The input field (type=number, name=ebot) watches for an onchange event and if detected calls a function. When I entered an invalid value and clicked 'submit' the form passed to php.

I then stripped down the function as indicated below to find out why.

function chkEvent() {
  var elemValue = document.forms['eFields']['eBot'].value;
  alert(elemValue);
}

Now entering anything into the input field (ebot) confirmed three (3) things:

  1. clicking submit fired ebot's onchange event and stopped the submit process
  2. the onchange event called the function chkEvent
  3. the alert box correctly displayed the user-entered value

I then restored chkEvent's original structure.

function chkEvent() {
  var elemValue = document.forms['eFields']['eBot'].value;
  if(elemValue <> 4) {
    document.eFields.esubmit.disabled=true;
    alert("Please solve correctly for x.");
  } else {
    document.eFields.esubmit.disabled=false;
  }
}

Now, regardless of what is entered in the spam filter, the form passes to php when submit is clicked. The same behavior exhibits in Chrome, Firefox, and IE. I may have stared at this for too long but please, no non-JavaScript suggestions. Time constraints prevent me learning a new language.

I've often benefitted from the tremendous skill and expertise on this forum. Thanx everyone !!

1
I will say what you're doing is likely going to block about 1% of your spam traffic as most spam bots don't even run a real browser, let alone obey JS code on the page that might block a form from submitting. They just scrape the form content, post the variables directly to your action="" script and move on to the next page. - Brian
What @Brian said. If anything, you should use JavaScript to submit the form, not block it. But then you lock out anyone who has JS disabled. - Niet the Dark Absol
Also try changing if(elemValue <> 4) { to if(elemValue == 4) { or perhaps changing the 4 to '4' I believe the type returned from a text input is a string, not an integer, so you could be running into a type issue there. - Brian
Your code isn't running because it contains syntax errors and the submit button's default behavior is taking place. Just look in your browser console. - JLRishe
@Brian I think s/he's going for elemValue !== '4'. - JLRishe

1 Answers

1
votes

<> is not a valid javascript operator. Use != instead, assuming you meant <> as "not equal to".