Parsley.js: never write a single JavaScript line to validate your forms
parsleyjs.org
parsleyjs.org
Other comments talked about client-side validation being mainly for UX and user convenience. But I'm wondering if it has a noticeable effect on whether or not people actually submit the form all the way to completion (i.e. no errors).
I made a tool that I have server-side form validation. I found that saving the invalid input so that I could examine it later was extremely valuable in understanding what people were entering, so that I could make the tool smarter. A good example is like with Parsley's demo where it requires http:// for the website URL. After seeing that people tried to enter URLs without http://, I knew I could change my code to automatically infer it. http:// is obvious, but a lot of times, people will surprise you. I don't have stats, but presumably this kind of change increases conversion. I realize this anecdote is a different effect than what I'm asking about client-side validation.
I can envision that could be easily to push this to Sentry https://getsentry.com/welcome/ using the Sentry Javascript Client
E.g. users at the time anyway, had massive problems with dropdown lists (a substantial number of errors were down to users who had clearly started typing, and got what the dropdown moved to as a result of them typing out the correct value). We ended up avoiding them (expiry dates in our billing form, for example).
(This was more than ten years ago, though, so our specific finding might very well be invalid now, with people more used to websites)
I'm not sure how this type of validation would play out. On one hand, if done well, it gives people immediate feedback. On the other hand it gives you no hints about what causes common mistakes like that. I like the idea of logging the values on error...
What I always find messy in web frameworks is doing validations which are more complicated.
For example a radio button choice which hides another form element which would otherwise be mandatory but should now not be filled in at all. Or where supplying a value between certain dates changes another date field from optional to mandatory and also must check that these dates are between certain values dictated by the first date field.
Though I'm not sure how much this could be generalised since complex validation rules are effectively computer programs in themselves.
Not sure what your approach to this problem is, but perhaps one way to do it would be a validation engine that is inherently functional by design.
Of course, ideally it should be something that can plug in to popular web frameworks easily.
It could be used in the same way as templating for javascript is used, a script element with a custom type or validation-src attribute in the form or something.
- The "website" field requires the http:// part of the address - After typing three letters into the "message" fieldarea, an error pops up and tells me the message needs to be at least 20 characters long, while I'm still typing away
I realize this could be tweaked by the user but the demo shouldn't have such logical flaws for a script that's described as "Super #UX focused". ;)
A couple of points:
1- Client side validation is easy to circumvent. If you use this you have to make sure it is for UI only and that the real validation happens server side.
2- Using regex for email validation is fraught with issues. Probably the most salient one is that there are a million expressions out there. People run a quick google search, grab one and move on without knowing what they just plugged into their code. This particular tool will, for example, pass "m@apple.comm" as valid.
Other than that, I am sure this is a great time saver.
2. True, but you have to do something. :) What exactly is wrong with m@apple.comm? That could be valid domain at some point, right? You could filter those out but it's almost a preference sort of question. Does anyone think it would make sense to have "warning" level of validation for email addresses and let the user decide on these sort of examples? Just throwing it out there.
I don't see a problem with using a regex to validate email, unless you get false negatives. Rejecting anything that could be a valid e-mail address is bad and will cause frustration for someone at some point.
I don't think allowing validly formed e-mail addresses that don't have corresponding accounts (or even domains - consider a web app working in offline mode) is a bad thing, especially when taken with point 1, as surely the point of client side validation is a heads up to the user that the data is wrong, rather than actual validation?
Say it's a signup form. Your validator lets a bad email through. The potential client/customer left. You can't email them to ask for correct email. They are gone. They may never come back (depends of what they were there for, right?). In that case I would want to do as much as might be reasonable in order to ensure that I capture someone who might eventually translate into revenue.
In other cases it might be just fine to store absolute gibberish and deal with it later. That said, if you have an incredibly popular site and are receiving thousands upon thousands of sign-ups per day, do you really want to store junk? Someone is going to have to go clean it up before it is of any use.
Anyhow, my main point, perhaps, is that one should understand what these magic regex email validators are and are not. That's all.
The reason I bring this up whenever relevant is because I have been bitten by this problem in the past. I only understood the problem when an existing customer informed me that they could not sign-up to receive info on a new product because my email validator was kicking them out. He just picked-up the phone and called me. I never did learn how many people I lost because of that damn regex I grabbed from an authoritative source on the 'net.
If you absolutely must have valid email addresses the only sure way is to make them confirm it with information from an email sent to the address.
Scenario:
I somehow find myself at your landing page.
You have a single field for may email and a "Sign me up!" button.
I enter my email and sign up.
Your regex lies to you by thinking that the email is fine. In reality I made a mistake when I typed it in. I didn't notice it. Neither did your regex.
How are you going to contact me?
OK, if it is a small shop you can probably afford to have a human being review bounced emails and try to make some sense out of them. Well, what if you are signing up a thousand people a day?
Anyhow, the point is that a bad email addresses can cost you money both in customers that might never come back and also potentially in the manual work required to try to fix them manually.
"異常.(),:;<>[]\".ຜິດປົກກະຕິ.\"அசாதரண@\\ \"অস্বাভাবিক\".არაჩ"@[IPv6:3ffe:1900:4545:3:200:f8ff:fe21:67cf] is a perfectly valid email address.
jQuery/Zepto $.data() was fast and easy to implement, rather than searching html5 attr.
I'll think about that ;)
Improve the demo by removing spellcheck from the name field and capitalizaton for ios (and likely other touch devices) where appropriate.
Along the same lines, demonstrate phone number field (number formatting), currency field, and automatically dealing with urls more nicely.
Javascript validation provides convenience, not security.
This may be premature optimization for a lot of cases but for form heavy apps this could be a useful optimization.
Also saving a round trip to the server can make the UX seem snappier and overall give a better user experience.
A hack attempt may be a direct POST circumventing any client side checks.
1) You can only do a mod 10 check (aka the Luhn algorithm) for client-side credit card validation. Beyond that, you'll need to hit a server to run a CC authorization.
2) In the email check one commenter criticized re: correctly parsing "m@apple.comm", there are practical limits. Syntactically, that's a correct email address. To that you could add content checks such as validating against known TLDs. Then you've got to ensure that your whitelist is correct and that it remains up-to-date as the TLD namespace changes. As for evaluating the registered domain name client-side, ehhhh.. maybe not. At some point you've just got to go do the DNS lookup and send an email to find out if it's going to work.
In general there are classes of problems for which some lightweight validation is practical on the client, but full validation requires extended information or transactions that are im{practical,possible} on the client.
http://www.codebyjeff.com/blog/2012/12/web-form-security-avo...
Nothing to do with sexy xss & the like - just some simple foundation points when setting up your server side procesing
Other than that, great job! I'm actually really looking forward to using this in my next project - form validation is usually such a pain.
If you like the concept of "extending html" as used here then you should probably checkout AngularJS: http://angularjs.org
At first glance, this looks like the best validation library I have seen. It so hard to find validation libs, that don't get into your way.
from here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-... <input class="text-box single-line" data-val="true" data-val-length="The field LastName must be a string with a maximum length of 60." data-val-length-max="60" data-val-required="The LastName field is required." id="LastName" name="LastName" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
Having just hit this situation (validating a form client-side), I found the best solution was to transport the form schema from the server (because I use Django, I can easily whip up a ModelForm), then push that into a simple Javascript Form object which renders the schema and adds validation. Maybe there's better ways, but that's what worked for me. Curious what others have done .. :-)
Your solution sounds nice for single page apps.
Yii framework (PHP) supports validation through Ajax (serializes the form, sends for regular server-side validation and displays the errors on the form). A godsend if your form does not change, and impossible to work with if it is dynamic (consider batch entries with "Add line" features, etc.).
I haven't seen a seamless integration on this higher level.
I ported django.forms to JavaScript so I could use it with Node.js, but it also runs on the client: https://github.com/insin/newforms
Could be useful to plug the two together.
I don't want to hijack, but I just want to link to the form library I wrote a couple years ago. It has a little less functionality, but it is a little lighter and has no dependencies. It is here: https://github.com/rickharrison/validate.js
I am working on a library which takes a data definition, renders the form and then validates it before submitting it via ajax. It supports various hooks like manipulate the data before submission etc.
IMHO form rendering (in a standard way) is also a common pain point along with form validation. Anyone interested in this type of library? Any inputs will be highly appreciated.
I'll look definitely into a more handy solution soon :)