❮ Previous Next ❯

Java script validate number

Java Script is used for validate number.

Here in the below example if you put 1 to 20 then it give out put 'Input OK'.

If you give more than 20 or less than 1 it give a out put that 'Input not valid'.

<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 1 and 20:/<p>
<input id="number">
<button type="button" onclick="myFunction()"Submit>
</button>
<p>
id="demo"</p>
<script>
function myFunction()
{
var x, text;
// Get the value of the input field with id="number"
x = document.getElementById("number").value;
// If x is Not a Number or less than one or greater than 20
if (isNaN(x) || x < 1 || x > 20) {
text = "Input not valid";
}
else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
------------------------------------------------------------------------------------------------------------------

HTML Form Validation

HTML form validation can be done automatically by the browser.

Just write "required" at the end of input tag.

< input type="text" name="fname" required>
<p>
"required" is a attribute.</p>
<!DOCTYPE html>
<html>
<body>
form action="demo.php" method="post">
<input type="text" name="fname" required>
<input type="text" name="lname" required>
<input type="submit" value="Submit">
</form>
<p>
If you click submit, without writing a single letter then it give a error message.</p>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------

JavaScript Form Validation

HTML form validation is also done by JavaScript.

In the below example if you not give any data in the first name it give a pop-up alert on the screen

Example- <!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x = document.forms["Form"]["fname"].value;
if (x == null || x == "")
{
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="Form" action="demo.asp">
<onsubmit="return validateForm()" method="post">
First Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved