Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
I have one textbox as follows

textbox1.text 100

In textbox1 when i enter age more than 100 means, shows the message you enter age between 1 to 100.


for that how can i validate using javascript.
Posted
Updated 14-Jul-21 22:00pm
Comments
Laiju k 5-Feb-18 3:25am    
Please try to search on google before posting here.

This question is trivial. Start by learning JavaScript programming so that you can help yourself in the long run.
1. Learn the basic first: http://www.w3schools.com/js/[^]
2. Then: http://www.w3schools.com/js/js_validation.asp[^]
 
Share this answer
 
v2
I hope this is use full for you..

XML
<script type="text/javascript">
       $(document).ready(function () {
           $("#Button1").click(function () {
               var res = document.getElementById("TextBox1").value;

               if (res<1 || res > 100)
                   alert("your message");
           })

       })
   </script>
 
Share this answer
 
Comments
Shivaram_i 25-Jun-15 1:56am    
2 Persons downvoted my answer..Before downvote my answer will you please give whats wrong in that answer.If you explain the wrong then i can improve my self..
Thanks
Laiju k 5-Feb-18 3:24am    
Please comment whats wrong before down voting.Simply down voting do not help anyone.
Hi,

Try the following code:
JavaScript
var age=document.getElementById('ageTextBox').value;

// Convert user entered age to a number

age = parseInt(age, 10);

//check if age is a number or less than or greater than 100
if (isNaN(age) || age < 1 || age > 100)
{ 
  alert("The age must be a number between 1 and 100");
  return false;
}


Hope this helps !!
 
Share this answer
 
Hi,

Write like this..

XML
First name: <input type="number" id="element_id">
<br>
<button type="button" id="Button1">Click Me!</button>



Javascript:

JavaScript
$(function () {
    $("#Button1").on("click", function () {

        if ($('#element_id').val() > 100 || $('#element_id').val() < 1) {
            alert("Please enter a number between 1 - 100 ");
        }

    });

});


Demo : click here
 
Share this answer
 
Using onkeyup event of the textbox we can show the message " enter age between 1 to 100 " when enter age more than 100.

Following code for this event

HTML
<label>Enter Your Age : </label>
<input type="text" id="txtAge" onkeyup="ageValidation()">

Javascript
function ageValidation() 
{
     var x = document.getElementById("txtAge").value;
     if (x < 1 || x > 100)
     {
            alert("enter age between 1 to 100")
     }
}
 
Share this answer
 
Comments
CHill60 30-Apr-20 11:26am    
Don't repost the same solution
Using onkeyup event of the textbox we can show the message " enter age between 1 to 100 " when entering age more than 100.

Following code for this event

HTML
<label>Enter Your Age : </label>
<input type="text" id="txtAge" onkeyup="ageValidation()">

Javascript
function ageValidation() 
{
     var x = document.getElementById("txtAge").value;
     if (x < 1 || x > 100)
     {
            alert("enter age between 1 to 100")
     }
}
 
Share this answer
 
Comments
CHill60 30-Apr-20 11:27am    
If you use on keyup you will get the alert after each digit entered. Absolutely the wrong event to tie this to.
SeeSharp2 14-Jul-21 13:50pm    
Took the words out of my mouth. +

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900