Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
JavaScript for only allow digits and in range 6-8

use in ASP.Net
and how to put in textbox.?

Ex. 123456
Ex. 12345678
Posted
Updated 20-Feb-15 22:47pm
v2
Comments
[no name] 21-Feb-15 4:22am    
What u mean?can u explain?

For this purpose, you can use an ASP.NET RangeValidator[^]:
ASP.NET
<asp:RangeValidator ID="digitRangeValidator"
        ControlToValidate="yourTextBox"
        MinimumValue="6"
        MaximumValue="8"
        Type="Integer"
        Text="The value must be from 6 to 8."
        runat="server"/>

And on the server, you can check whether the input is valid: if digitRangeValidator.IsValid is true, then the input is valid.
 
Share this answer
 
Comments
Wendelius 21-Feb-15 4:27am    
Good point.
You can do one of the following:

1) Use Asp.Net's range validator.
https://msdn.microsoft.com/en-us/library/f70d09xt(v=vs.71).aspx[^]

2) Or if you are a JavaScript-UI ninja then do something like this:
XML
<input type="text" id="txtNumber" runat="server"/>
    <script>

        document.addEventListener('DOMContentLoaded', function () {
            var textBox = document.getElementById('txtNumber');
            textBox.onchange = validateNumbeRange.bind(textbox, 6, 8);
        });

        function validateNumbeRange(min, max) {
            var value = parseFloat(this.value);

            if (isNaN(value) || !(!isNaN(value) && value >= min && value <= max))
                alert('Invalid value');
        }
    </script>


The above is calling a function on textbox value change and then validating the value.
 
Share this answer
 
Comments
Karmesh_Madhavi 21-Feb-15 4:36am    
what is DOMContentLoaded.?
what is there put somthing...????
Nitij 21-Feb-15 5:04am    
DOMContentLoaded is an event which is fired when document schema is loaded and it is Html5 compatible. I forgot to mention this before but the 2nd solution will not work on the server side, so you also need to validate the value in your c# code if you decide to use this approach.

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