Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,

I want is like , I have text box and it should not allow me to enter characters but at the same time it should allow me to enter only digits which should not be 4 digits

JavaScript
function check(s, e) {
       debugger;
       var theEvent = e.htmlEvent || window.event;
       var key = theEvent.keyCode || theEvent.which;
       key = String.fromCharCode(key);
       var regex = /^\d{4}$/;

       if (!regex.test(key)) {
           theEvent.returnValue = false;
           if (theEvent.preventDefault)
               theEvent.preventDefault();
       }
   }


I am using devexpress aspx text box ..
I can use mask settings but this is not working with type password
Posted
Updated 17-Dec-15 0:43am
v5
Comments
phil.o 17-Dec-15 6:12am    
What have you tried? Where are you stuck?
Torakami 17-Dec-15 6:34am    
I have updated my question

This is I am trying to check on keypress , but there I dont think it will work . as whatver i type it will not match the criteria

What I want is .. I dont want user to add charctrs , but he can add numbers . but numbers should not be more than 4 digits
F-ES Sitecore 17-Dec-15 7:15am    
Validate the password server-side and if it's of the wrong format return the user to the page and give them a message. Remember the user can disable js so avoiding your attempts to validate their password.

two options.

A: In html5 you can set a maxlength. you will then have to check on change what the values entered are and restrict it to 4

B: In html5 you can set the textbox to textmode="number" which will restrict the input to numbers only (and will add a number iterator on the right) You will then have to check the textbox changed to check for the digits length

I don't know why, but these two methods don't work together. Either way you will need to check the input in javascript:

quick and easy:
HTML
<input type="text" maxlength="4" onkeyup="this.value = this.value.replace(/[^0-9]/, '')" />


PS: I advise using the jQuery "change" event as that covers copy-paste as well and keystrokes

Hope that helps ^_^
Andy
 
Share this answer
 
Comments
Torakami 17-Dec-15 9:23am    
can we make the same for devexpress textbox .. I am trying for same but for some reason there I am getting eror stating this is invalid object
Andy Lanng 17-Dec-15 9:27am    
Ah, yeah - DevExpress. Good luck :Þ
You will prolly have to use a different control. Take a look at this post:
https://www.devexpress.com/Support/Center/Question/Details/Q206540
Torakami 18-Dec-15 5:22am    
Thanks for you answer ,,, I used it for devexpress from that ..

check my solution as well
<dx:ASPxTextBox ID="txtPinNumber" ClientInstanceName="txtPinNumber" CssClass="cpTextbox" Text="" runat="server"
Password="true" MaxLength="4">
<clientsideevents keyup="CheckPinNumber">


C#
function CheckPinNumber(s, e) {
       if (null != s.GetValue() && '' != s.GetValue()) {
           s.SetValue(s.GetValue().replace(/[^0-9]/, ''));
       }
   }
 
Share this answer
 
v2

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