How do I do client-side form validation using JavaScript or jQuery?
-
Hi, I'm doing a project on CodeCademy and it has asked me to do a client-side Form Validation using HTML/CSS and JavaScript/jQuery. I know how to validate things using JavaScript but only on prompt. The main problem I have is that how do I set JS to to take the value of the input? For Example: I have to check whether the user has input the age in numbers or not. Same goes for the name, to check if it's a String or not. Also, to check whether a field has been left empty or not. There will be a button, on click it'll check all fields and if any is empty or wrong it'll show an alert. Please input the full code.
-
Answer:
First of all, all data sanitization and validation should be done on the server-side, not the client-side. The main reason being where you can protect your code (anyone can edit your JS on the client-side). That said, it becomes a question of "how do I write basic JavaScript code that forces required form field input?" There's a few ways to do it. I'll put down one way that works nicely with a concrete example everyone usually has to do, at one point or another -- telephone validation. HTML5 provides, for inputs, a way to allow this validation to be done somewhat automatically. Let's assume for some reason, that it works: <input type="tel" name="contact[phone]" id="phone" required pattern="^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$"> Then this is all you need. It's VERY, VERY, VERY flexible. For other regular expressions -- see this: http://stackoverflow.com/questions/1449817/what-are-some-of-the-most-useful-regular-expressions-for-programmers. Now the incredibly nice part about providing an actual regular expression within the pattern is that it provides a nice fallback allowing us to handle even more shitty browsers (IE). var input = document.querySelector('#phone'); isValid = input.value.search(new RegExp(input.getAttribute('pattern'))) >= 0; and you're done. It's pretty straightforward, I think. This is all with vanilla javascript, and it's very nice. If you're wondering about browser support -- look into http://modernizr.com/.
Giordon Stark at Quora Visit the source
Other answers
I concur with everything Giordon Stark wrote. But maybe your are missing the part where the validation is triggered? In very old style Javascript you could write: <script> function check_the_values() { var v = document.getElementById('number').value; if (isNaN( parseInt(v) ) ) { alert("please input a number!"); return false; } else { return true; } } </script> <form onsubmit="return check_the_values();"> <input id="number" name="number" /> <input type="submit" /> </form> Before the Form is submitted to the server the "submit" event is triggered. Here we add a handler to that event by using the "onsubmit" attribute in the form tag. If the handler returns true, the form will actually be submitted. if the handler returns false no data will be submitted.
Brigitte Jellinek
Use jQuery Validator. It's simplest and fastest.
Sandeep Kamlesh Mishra
Related Q & A:
- How do I remove a site from IIS7 using JavaScript?Best solution by Server Fault
- How do I upload a picture instead of using a generic avatar?Best solution by answers.yahoo.com
- How do I uninstall Client Services for NetWare?Best solution by Yahoo! Answers
- How can I find an automatic form filler on Yahoo?Best solution by Yahoo! Answers
- How do I output from one form to another in Visual Basic?Best solution by support.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.