Shifat Shifat - 1 year ago 72
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 Source

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 
    return true
<input type="text" id="number" class="inputnw" name="semesterCode"  onkeyup="myFunction()"/>
<span id="error" style="color:red"></span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download