Search code examples
javascripthtmljquery-validation-engine

need to return true or false based on if value in text field is a specific number


I am looking to return true if the user entered 4 into the input field.

function validateAddition() {
  if($("#validateAddition").val() == 4) {
    return true;
  } else {
    return false;
  }
}

<input value="" 
class="validate[required,onlyNumber,length[0,1]funcCall[validateAddition]] text-input" 
type="text" id="validateAddition" name="validateAddition" />

Added this into the english language js file:

"validateAddition":{
    "nname":"validateAddition",
    "alertText":"* Your math is off, try again."}

This should be fine, let me know what you think is wrong.


Solution

  • http://www.w3schools.com/jsref/jsref_parseInt.asp

    return (parseInt($('#validateAddition').val()) == 4)
    

    EDIT: MooGoo is correct, I was mistaken as far as my explanation.

    However is idea of using +val is probably not the best. While the method above WILL work as you require a much better solution would be as follows:

    if(($('#validateAddition').val()-0) == 4)
    

    The reason we want to -0 as opposed to +0 is simply because if it is a string and you want to perform any additional operations incase it is, you will no longer have the original string.