1

I am familiar with a number of options for validation, and in fact tend to do it both client side and server side, for obvious reasons.

I have some very good perl packages for validating everything I need to validate. What I haven't found is corresponding data validation packages for Javascript. For phone numbers, I see a library: https://github.com/googlei18n/libphonenumber, but it is not clear how I would use it in a Javascript function. Similarly, I have found a number of regular expressions for validating a postal code given the ISO country code in the address. But there are well over 100 of them and I shudder to think of entering them all and then figuring out which to apply given he ISO country code.

Now, are there good quality Javascript libraries that I can use to validate e.g. phone numbers and postal codes (and credit cards and CVV values), or am I wasting my time looking for that when I can resort to ajax calls that make use of my Perl packages?

  • *Always* validate on the server. If you want to improve the user experience, you can also validate on the client, but nothing forces you to do client-side checks. – ThisSuitIsBlackNot Mar 04 '16 at 21:39
  • @ThisSuitIsBlackNot, They're asking for the best way to do client-side checks. – ikegami Mar 04 '16 at 21:51
  • i've see things to run perl in JS... https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js#perl that would probably work on something simple like validating a string. – dandavis Mar 04 '16 at 22:52
  • Thanks guys. I would emphasize, for the benefit of readers who see this dialog, my ideal is to do data validation BOTH client side and server side. I do the validation server side for obvious reasons,, and I do it client side to enhance user experience, especially to save the user time and effort by notifying him or her of data entry error at the earliest possible moment. What has been problematic for me has been that while I have an excellent suite of perl packages for data validation, I have not yet found a comparable suite of javascript libraries. To be able to run Perl in JS has appeal. – user1289485 Mar 05 '16 at 20:59

1 Answers1

2

The Browser is behind enemy lines, anything coming from it should be treated with extreme caution. When it comes to browser side validation it should be considered User Experience only even if a well known JS library is used to validate the input. You must always check it server side. It appears you have these services already which is great.

The last paragraph is not necessarily for the OP, it's for anyone coming to this problem for the first time because it's a fundamental concept ie Client side validation of things like credit cards etc is a fountain of security problems.

Now we know we need robust server side validation. In my experience this simplifies things because quite often you can find simple JS libraries that will perform 98% of the use cases you need and for anything else you fall back to the service.

Two cases in point.

  1. US zip codes can be handles in part using a few lines of code

Angular ng-pattern regex code for US Zipcodes and Canadian Postal codes

  1. CVV numbers and credit cards, If you adopt something like angular (and I'd recommend it) you can take advantage of any number of libs.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=angular+credit+card+

In short, if you have robust server side validation the question becomes about user experience only. If your services perform great then use them to do the validation using Ajax, if you want to make the site feel more like a single page app use JS libraries and fall back to services. The only thing you must never do is trust what you get from behind enemy lines.

Community
  • 1
  • 1
Harry
  • 11,298
  • 1
  • 29
  • 43
  • Thanks Harry, I generally agree with you.My only uncertainty WRT ajax+perl is how well it scales.Let me ask you three questions. 1) Do you know of a JS library that is useful globally rather than merely for north america? 2) related to this is if/how this JSON, https://gist.github.com/matthewbednarski/4d15c7f50258b82e2d7e can be used in JS, and my uncertainty as to how to use that to make a function that takes an ISO country code, along with the postal code value, to validate the postal code value. 3) do you now if/how https://github.com/googlei18n/libphonenumber can be used from within JS? – user1289485 Mar 05 '16 at 21:19
  • I don't know of anything off hand but you can find various libs like https://github.com/jquery/globalize in this space. i18n done well is difficult. Personally I'd look at what the international web companies are using ie Yahoo's YUI would also be a good place to start http://yuilibrary.com/yui/docs/intl/. Or anything that Facebook/Twitter/Google etc are using will likely be suitable. To use the libs from within JS it's a case of reading and more reading, the libphonenumber page has some quick examples that show you how to use the country code and check numbers. – Harry Mar 06 '16 at 00:45