Shifat Shifat - 4 days ago 5
Javascript Question

Input validation for Number (user cant input less than 163)

I want to restrict user from giving value less than 163.
I already tried

<input type="text" class="inputnw" name="semesterCode" min="163" value="163" />


I have used text as i dont want to show increment/decrement at the right side of input box.

I want validation for anything below 163 and User can only type numeric number.

Answer

You can use this script. Whenever you enter a number less than 163 it will give an alert saying "Number must be less than 163"

function myFunction(){
  var x = document.getElementById("number").value;
  if ( x.length > 2 && x < 163 ) {
    document.getElementById("error").innerHTML = "Please enter a number less than 163"
    return false 
  }else{
    return true
  }
}
<input type="text" id="number" class="inputnw" name="semesterCode"  onkeyup="myFunction()"/>
<span id="error" style="color:red"></span>

Comments