Restricting input with jQuery
If there’s one thing that drives me insane online, it’s when input forms allow me to enter incorrect data, only to point out the mistake after I try and submit it. It seems like half the forms I submit have to be refilled and submitted over again because I didn’t include an uppercase letter in my password, or I did, or the password can only be numerical, or some other requirement nobody thought to mention.
The way the brain works, we look for solutions based on the tools in front of us. You don’t enter uppercase letters at the ATM do you? No, because the ATM keypad only has numbers. You might hit the wrong number by mistake, but you’ve never tried to enter your email address, or your mother’s maiden name.
Therein lies the problem, the keyboard that you use complicates inputting data online. It probably has between 75 and 100 keys and even more characters are easily accessible by holding multi-key combinations. Using it to log into Facebook is rather like popping out for milk in a Ferrari.
Of course, your keyboard has to have more input options than any particular form field requires, because it’s a multi-use tool; you can’t practically have a different keyboard for every possible type of input.
This leads to a serious usability issue: users are constantly being asked to ‘correct’ their information to suit a form. That’s a great way to increase frustration and lose business.
Touchscreen devices have made great strides in this area by modifying the onscreen keyboard to tailor the types of input possible, to the data required; enter an email address on an iPhone for example, and you won’t be able to enter a space by mistake, because the spacebar isn’t provided.
Digital keyboard image via Shutterstock
Until we’re all working on touchscreens, we need a temporary solution, and there’s actually quite a simple one: using jQuery we can slip a layer of intelligence between the keyboard and the input field and only accept the data if it falls within expected bounds, ignoring anything outside those bounds, confident that it’s an error.
First, we need to set up an input field in HTML that we want to restrict, a phone number for example:
Then, in the document’s head, we need to import jQuery:
And immediately afterwards add the following script:
This script runs once the document is ready, attaching a keypress method to the .phoneInput input field. We then detect which key has been pressed based on its charCode property — the number 0 is assigned the code 48, 1 is 49 and so forth — any key that’s outside our range should return false. If the method returns false the browser will simply ignore the keystroke.
This means that if the user hits any key that isn’t 0–9 the input will be ignored, effectively restricting the input to numbers.
We can apply the same technique to almost any field, building up complex rules using logical AND and logical OR. For example, if we wanted to restrict input for a surname, we would need to restrict input to lowercase letters (97–122), uppercase characters (65 – 90) and the occasional hyphen (45):
$('.surnameInput').keypress(function(key) {
if((key.charCode 122) && (key.charCode 90) && (key.charCode != 45)) return false;
});
You can try out a demo here.
This code is a progressive enhancement. It will take some of the strain off your server validation, but that doesn’t mean that you shouldn’t also validate information you’re gathering.
Prevention, as the saying goes, is better than cure; and using this tip you’ll see a reduction in the number of people who start, but don’t complete your forms, especially when complex data requirements are involved.
Do users regularly make mistakes on your forms? How do you handle the mistakes? Let us know in the comments.
Featured image/thumbnail: keyboard image via Shutterstock
10 Cartoon Characters in 400+ poses – only $24! |
2012-10-29 11:49:08
Source: http://www.webdesignerdepot.com/2012/10/restricting-input-with-jquery/
Source:
Anyone can join.
Anyone can contribute.
Anyone can become informed about their world.
"United We Stand" Click Here To Create Your Personal Citizen Journalist Account Today, Be Sure To Invite Your Friends.
Before It’s News® is a community of individuals who report on what’s going on around them, from all around the world. Anyone can join. Anyone can contribute. Anyone can become informed about their world. "United We Stand" Click Here To Create Your Personal Citizen Journalist Account Today, Be Sure To Invite Your Friends.
LION'S MANE PRODUCT
Try Our Lion’s Mane WHOLE MIND Nootropic Blend 60 Capsules
Mushrooms are having a moment. One fabulous fungus in particular, lion’s mane, may help improve memory, depression and anxiety symptoms. They are also an excellent source of nutrients that show promise as a therapy for dementia, and other neurodegenerative diseases. If you’re living with anxiety or depression, you may be curious about all the therapy options out there — including the natural ones.Our Lion’s Mane WHOLE MIND Nootropic Blend has been formulated to utilize the potency of Lion’s mane but also include the benefits of four other Highly Beneficial Mushrooms. Synergistically, they work together to Build your health through improving cognitive function and immunity regardless of your age. Our Nootropic not only improves your Cognitive Function and Activates your Immune System, but it benefits growth of Essential Gut Flora, further enhancing your Vitality.
Our Formula includes: Lion’s Mane Mushrooms which Increase Brain Power through nerve growth, lessen anxiety, reduce depression, and improve concentration. Its an excellent adaptogen, promotes sleep and improves immunity. Shiitake Mushrooms which Fight cancer cells and infectious disease, boost the immune system, promotes brain function, and serves as a source of B vitamins. Maitake Mushrooms which regulate blood sugar levels of diabetics, reduce hypertension and boosts the immune system. Reishi Mushrooms which Fight inflammation, liver disease, fatigue, tumor growth and cancer. They Improve skin disorders and soothes digestive problems, stomach ulcers and leaky gut syndrome. Chaga Mushrooms which have anti-aging effects, boost immune function, improve stamina and athletic performance, even act as a natural aphrodisiac, fighting diabetes and improving liver function. Try Our Lion’s Mane WHOLE MIND Nootropic Blend 60 Capsules Today. Be 100% Satisfied or Receive a Full Money Back Guarantee. Order Yours Today by Following This Link.
