{"id":4494,"date":"2011-11-14T18:17:19","date_gmt":"2011-11-14T12:47:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4494"},"modified":"2011-11-14T18:18:14","modified_gmt":"2011-11-14T12:48:14","slug":"validating-emails-urls-and-date-using-java-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/validating-emails-urls-and-date-using-java-api\/","title":{"rendered":"Validating emails, urls and date using Java API"},"content":{"rendered":"<p>Recently I was looking for a programmatic way for validating data against well known validation e.g. email and date. I have to use them inside custom validator and sometimes in controller\/action. Creating Command objects for validating was not suitable in my case(overkilling solution). Writing\/looking for regular expression was another solution. I found a special class  <code>GenericValidator<\/code> in <code>org.apache.commons.validator<\/code> package (<code>commons-validator<\/code> jar file, bundled with grails jars) . The class has many useful <code>static<\/code> methods for validating data. e.g. for validating <em>email, url, credit card, date<\/em> etc. Here is the sample code for validation using <code>GenericValidator<\/code> class: <\/p>\n<p>[groovy]<br \/>\nimport org.apache.commons.validator.GenericValidator<\/p>\n<p>assert GenericValidator.isEmail(&quot;bhagwat@intelligrape.com&quot;)  \/\/valid email<br \/>\nassert !GenericValidator.isEmail(&quot;bhagwatintelligrape.com&quot;)  \/\/ invalid email<\/p>\n<p>assert GenericValidator.isUrl(&quot;http:\/\/www.intelligrape.com&quot;)  \/\/valid URL<br \/>\nassert !GenericValidator.isUrl(&quot;www.intelligrape.com&quot;) \/\/invalid URL<\/p>\n<p>assert GenericValidator.isCreditCard(&quot;4111111111111111&quot;) \/\/ valid visa card number<br \/>\nassert !GenericValidator.isCreditCard(&quot;4111111111111112&quot;) \/\/ invalid visa card number<\/p>\n<p>assert GenericValidator.isDate(&quot;2011-12-30&quot;, &quot;yyyy-MM-dd&quot;, true) \/\/ valid date(last argument for checking strict)<br \/>\nassert GenericValidator.isDate(&quot;2011-02-28&quot;, &quot;yyyy-MM-dd&quot;, true) \/\/ valid date &#8211; 28th Feb<br \/>\nassert !GenericValidator.isDate(&quot;2011-02-29&quot;, &quot;yyyy-MM-dd&quot;, false) \/\/ invalid date &#8211; 29th Feb 2011<br \/>\n[\/groovy]<br \/>\nFor other useful methods and their details follow the link :<br \/>\n<a href=\"http:\/\/commons.apache.org\/validator\/apidocs\/org\/apache\/commons\/validator\/GenericValidator.html\">http:\/\/commons.apache.org\/validator\/apidocs\/org\/apache\/commons\/validator\/GenericValidator.html<\/a>.<\/p>\n<p>In addition to <code>GennericValdator<\/code> class there are separate classes for validating date, email, credit card, ISBN validator in the <code>org.apache.commons.validator package<\/code> viz. <em>DateValidator, CreditCardValidator, EmailValidator, ISBNValidator, UrlValidator<\/em> and so on. Here is the sample code for using <code>DateValidator<\/code> class.<br \/>\n[groovy]<br \/>\nimport org.apache.commons.validator.DateValidator<\/p>\n<p>DateValidator dateValidator=DateValidator.getInstance()<br \/>\nassert dateValidator.isValid(&quot;11-02-28&quot;, &quot;yy-MM-dd&quot;, true)<br \/>\nassert !dateValidator.isValid(&quot;11-02-29&quot;, &quot;yy-MM-dd&quot;, true)<br \/>\n[\/groovy]<\/p>\n<p>Hope it helps you.<\/p>\n<p>Bhagwat Kumar<br \/>\nbhagwat(at)intelligrape(dot)com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using commons-validator jar for validation emails, date, url, credit card etc. This jar file is bundled with the grails jar files. So writing regular expressions or creating command objects\/domains for the single field validation is overkilling. The jar file has a special class GenericValidator for validating well-known data types.<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[680,679,676,677,675,681],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4494"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4494"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4494\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}