-1

Here i am trying to do the validation for the html input form. here i want to return false if the input is not a number. i want to return false if anything else other than number is supplied as input.

How can i do this? what is the pattern for the number validation

<form class="form-horizontal" name="product-form" method="post" action="go.php" role="form" enctype="multipart/form-data" onsubmit="return validateForm();">

<div class="form-group">
                <label class="control-label col-sm-2" for="product_price">Product Price</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" name="product_price" id="product_price" placeholder="36000" required>
                </div>
              </div>
</form>

Javascript

function validateForm() {
    var x = document.forms["product-form"]["product_price"].value;
    if (x == null || x == "") {
        alert("price must have only numbers");
        return false;
    }
}
CJAY
  • 6,989
  • 18
  • 64
  • 106

1 Answers1

0

Or you can use HTML5 input type as number

<form class="form-horizontal" name="product-form" method="post" action="go.php" role="form" enctype="multipart/form-data">

<div class="form-group">
                <label class="control-label col-sm-2" for="product_price">Product Price</label>
                <div class="col-sm-10">
                  <input type="number" class="form-control" name="product_price" id="product_price" placeholder="36000" required>
                </div>
              </div>
</form>
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55