Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable !, @, #........ 1

Status
Not open for further replies.

arpan

Programmer
Joined
Oct 16, 2002
Messages
336
Location
IN
I have a textbox in a HTML Form where I want that users should only enter dates. That's why I want that users should only be allowed to enter numbers from 0-9 & the '/' character in the textbox. This is how I am doing using the onKeyDown event:

<script language=&quot;JavaScript&quot;>
function handler(e) {
var key=e.keyCode;
if(key==13 || key==46 || key==191 || (key>47 && key<58))
return true;
else
return false;
}
</script>
<form>
<input type=&quot;text&quot; onKeyDown=&quot;return handler(event)&quot;>
</form>

The above code will ensure that even if a user tries to type some letters (a, b, c, d.......), it won't be reflected in the textbox. But the above code does not take care of the following characters - !, @, #, $, %, ^, &, *, ( & ) i.e. the characters that are typed using the 'Shift' key + any number key (for e.g. Shift + 1 will type !, Shift + 2 will type @ etc...). How do I ensure that the users are not allowed to enter these special characters that are usually typed by pressing 'Shift' + any number key?

Thanks,

Arpan
 
<script language=&quot;JavaScript&quot;>
function handler(e) {
var key=e.keyCode;
if (e.shiftKey) return false;
if(key==13 || key==46 || key==191 || (key>47 && key<58))
return true;
else
return false;
}
</script>
<form>
<input type=&quot;text&quot; onKeyDown=&quot;return handler(event)&quot;>
</form>

Regards,
Sergey Smirnov
 
Thanks, mate, for the solution.

Regards,

Arpan
 
Hi Harmmeijer,

There's no doubt that the suggestion you have given is indeed a very good one but there are a couple of aspects for which I wouldn't take this approach. First of all, in order to set a date, users have to click a button to wait for the new page to open in a new window which means retrieving that page from the server which, in turn, means users have to wait for an additional extra bit of time which can be quite frustrating in case if the connection speed is a slow one. Secondly, suppose if, by mistake, a user selects a wrong date & wants to modify it. He has to again click the button to change the date & wait for the new page to be retrieved from the server in the new window. Lastly, following your approach means incorporating a large chunk of JavaScript code which I would like to avoid.

But let me once again reiterate that there's no doubt that the suggestion you have provided is a good one. Please do not be under the impression that I am trying to demean you or your work in anyway by just pointing out the drawbacks of the idea you have provided.

Regards,

Arpan :) :) :)
 
Here are the reasons why I never let users fill out a date in a textbox.

I am working in an office were users are used to dateformats like
mm/dd/yyyy and others use dateformats like
dd/mm/yyyy.

If I would make a reservation system this could get unexpected results because one user thinks 1/2.. is first of feb and the other thinks is second of jan.

If the user has filled out the wrong date the users has to wait for the round trip to the Internet and database server to find out the date is not valid (or wurse: wrong) and fill out the entire form again.

The java calendar is 18k and is not an asp so it can be in the user's history (it will not reloaded from the server the second time).
 
I agree with harmmeijer about using text boxes for dates and for the same reason. I always use three select boxes clearly labelled has to which is the day, month and year. Ok you may require a bit of javascipt to check if valid date (no 31st November etc), but it works fine. --------------------------------
If it ain't broke, don't fix it!
 
I do not ready to share suggestion about three fields (especially, selected boxes) as a best way to input date. If your application is such kind of backoffice and operators has to enter date hundreds times a day, customer will tears your to pieces for three fields. Operator usually spends less then second to enter date using numeric keyboard. Three fields will be a guillotine for him/her.

Regards,
Sergey Smirnov
 
I agree with sim777. Surely it is the developers job to make life easier for the user, not to produce systems that are easier to code?

Sorry for going O/T :)
 
In input mask is the best way if you need to input rows and rows of data.
But how do you make an input mask on a html text input?

This is after all the javascript forum not the excel forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top