Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i want user to enter till 0-59 number in html text box. how to do uing javascript and how to call same java script function to same HTML text box.


please reply me immediate......pls
Posted
Updated 19-Nov-13 0:04am
v2

Hi Kamal,

Try this code,
Here is the javascript
XML
<script type="text/javascript">
        function validateTextBox() {
            var obj = document.getElementById('txtBox');
            if (obj.value > -1 && obj.value < 60) {
                return true;
            }
            else {
                alert('Range should be in 0-59');
                return false;
            }
        }

    </script>

And in html you need to call it in onBlur event, on losing its focus it will throw error.
You can change(onKeypress,onKeydown or something) when you want to throw the alert that depends on you.
HTML code:
<input type="text" id="txtBox" onblur="validateTextBox()"/>


I hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
Matt T Heffron 19-Nov-13 13:16pm    
Why not use the same constant values as reported in the alert?
if (obj.value >= 0 && obj.value <= 59) {
♥…ЯҠ…♥ 19-Nov-13 23:02pm    
why? anything wrong in that condition? both are same only right?
Matt T Heffron 20-Nov-13 13:00pm    
There's nothing wrong with it...but it seems that the code would be easier to understand and maintain if the values you were checking as limits were the same as you were reporting.
C#
var obj=document.getElementbyid('txtbox1');
if(obj.value >-1 && obj.value< 60)
{
return true;
}
else
{
return false
alert('enter b/w 0-59 only');
}
 
Share this answer
 
v2
Comments
CHill60 19-Nov-13 6:38am    
Correct me if I'm wrong but that alert is after the return so will never be executed?

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