Click here to Skip to main content
16,016,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI ,
i want to validate in my application tht it shd display message enter number if i enter a character instead of number ....
Posted

XML
<!DOCTYPE html>
<html>
<head>
<script>
    function changeFunction() {
        var reg = /[1-9]/g
        input_val = document.getElementById("input_box_id").value
        if(!reg.test(input_val) ){ //true if non numeric value
            alert("Please enter valid number");
        }
    }
</script>
</head>
<body>

Number : <input type="text" id="input_box_id" onchange="changeFunction()">

</body>
</html>
 
Share this answer
 
Comments
Member 11704156 21-May-15 6:08am    
do i need to write anything in code behind? for this function
If you're using ASP.NET 4.0 or later, you just need to add type="number" to your TextBox:
aspx
<asp:TextBox runat="server" type="number" />


This will render a numeric input type:
http://www.html5tutorial.info/html5-number.php[^]

It's supported in all modern browsers:
http://caniuse.com/#feat=input-number[^]

If you need to support older browsers, there is a polyfill available:
https://github.com/jonstipe/number-polyfill[^]

This has the added benefit that, on mobile devices, the user will see the numeric keyboard by default when they tap on the input.
 
Share this answer
 
Comments
Member 11704156 22-May-15 1:30am    
still i am not getting the alert message like please enter number if i giv alphabet ..whats the solution .. wt to code using javascript
Richard Deeming 22-May-15 8:18am    
Displaying an alert message every time the user presses a key you don't like is a very bad idea. It's just going to annoy the users.

Using a numeric input, the user either won't be able to enter non-numeric characters, or will get a validation warning if they do:
Example[^]

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