<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: validation code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:14:14 GMT</pubDate>
    <description>DZone Snippets: validation code</description>
    <item>
      <title>jquery validation not clearing errors example</title>
      <link>http://snippets.dzone.com/posts/show/5700</link>
      <description>// jquery validation example not clearing error messages&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;  &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;  &lt;script type="text/javascript"&gt;&lt;br /&gt;  $(document).ready(function(){  &lt;br /&gt;    $("#command").validate({&lt;br /&gt;      errorPlacement: function(label, element) {        &lt;br /&gt;        //$(element).prev(".error").replaceWith("");&lt;br /&gt;        label.insertBefore( element );&lt;br /&gt;      },    &lt;br /&gt;    &lt;br /&gt;      rules: {      &lt;br /&gt;        "profileBean.loginEmail": {&lt;br /&gt;                required: true,&lt;br /&gt;                minlength: 5,&lt;br /&gt;				email:true&lt;br /&gt;            },    &lt;br /&gt;        "password": {&lt;br /&gt;        					required:true,&lt;br /&gt;        					minlength:5&lt;br /&gt;        			},&lt;br /&gt;		"passconfirm":{&lt;br /&gt;		                    required:true,&lt;br /&gt;		                    minlength:5,&lt;br /&gt;		                    equalTo:"#password"&lt;br /&gt;		}&lt;br /&gt;      },&lt;br /&gt;      messages:{&lt;br /&gt;          "profileBean.loginEmail":{&lt;br /&gt;                              required:"Email must be supplied",&lt;br /&gt;                              minlength:"specify at least 5 characters",&lt;br /&gt;          }&lt;br /&gt;       }&lt;br /&gt;    });  &lt;br /&gt;  });&lt;br /&gt;  &lt;/script&gt;  &lt;br /&gt;  &lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;   &lt;table &gt;&lt;br /&gt;&lt;br /&gt;        &lt;form id="command" name="registerForm" action="www.cnn.com" method="POST"&gt;&lt;br /&gt;    &lt;tr&gt;&lt;td align="center"&gt;&lt;/td&gt;&lt;/tr&gt;                      &lt;br /&gt;    &lt;tr&gt;                &lt;br /&gt;        &lt;td &gt;&lt;span class="spanlabel"&gt;Email Address&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;input id="profileBean.loginEmail" name="profileBean.loginEmail"  type="text" value=""/&gt;&lt;/td&gt;        &lt;br /&gt;        &lt;/tr&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;&lt;br /&gt;        &lt;td &gt;&lt;span class="spanlabel"&gt;Password&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;input id="password" name="profileBean.password" type="password" value=""/&gt;&lt;/td&gt;        &lt;br /&gt;        &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;        &lt;td &gt;&lt;span class="spanlabel"&gt;Re-enter password&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;input type="password" id="passconfirm" name="passconfirm" /&gt;&lt;/td&gt;&lt;br /&gt;        &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;        &lt;td &gt;&lt;br /&gt;        &lt;input type="submit" name="submit" /&gt;&lt;/td&gt;&lt;br /&gt;        &lt;/tr&gt;&lt;br /&gt;        &lt;/form&gt;&lt;br /&gt;        &lt;br /&gt;     &lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 Jun 2008 16:38:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5700</guid>
      <author>joshjdevl (Josh)</author>
    </item>
    <item>
      <title>Rails validation for Phone</title>
      <link>http://snippets.dzone.com/posts/show/5057</link>
      <description>&lt;code&gt;&lt;br /&gt;  validates_length_of :phone, :is =&gt; 10, :message =&gt; 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if =&gt; Proc.new{|o| !o.phone.blank?}&lt;br /&gt;  validates_length_of :fax, :is =&gt; 10, :message =&gt; 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if =&gt; Proc.new{|o| !o.fax.blank?}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 31 Jan 2008 00:38:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5057</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Mark directories to be deleted based on their date-stamp</title>
      <link>http://snippets.dzone.com/posts/show/4934</link>
      <description>This Ruby code selects file directories which are older than a certain date and outputs an XML file naming all the directories to be removed.  It does this by reading a directory listing formatted within the XML file 'dir.xml', all directories are named by a date-stamp, which is used to determine if the directory should be removed.&lt;br /&gt;&lt;br /&gt;This example is used to maintain the webcamera (named 'pear') which saves it's images to a date-stamped directory daily. Any directory which is older than 14 days will be marked for deletion.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def directory_housekeeping()&lt;br /&gt;    lifespan = 14&lt;br /&gt;    format_mask = 'm_d_y'&lt;br /&gt;    separator = format_mask.match(/[\_*\-]/).to_s&lt;br /&gt;    &lt;br /&gt;    earliest_date = Time.now + (60 * 60 * 24) * -lifespan&lt;br /&gt;    cut_off_date = Date.new(y=earliest_date.year,m=earliest_date.month,d=earliest_date.day)&lt;br /&gt;        &lt;br /&gt;    a_format = Array.new&lt;br /&gt;    a_format[0] = format_mask.match(/^[y,m,d]*/).to_s&lt;br /&gt;    a_format[1] = format_mask.match(separator + '[y,m,d]*').to_s.gsub(separator,'')&lt;br /&gt;    a_format[2] = format_mask.match('[y,m,d]$').to_s&lt;br /&gt;    &lt;br /&gt;    file = File.new('../housekeeping/webcam_pear/dir.xml')&lt;br /&gt;    ddoc = REXML::Document.new(file)&lt;br /&gt;    file.close&lt;br /&gt;    &lt;br /&gt;    file_delete = File.new('../housekeeping/webcam_pear/files2delete.xml', 'w')&lt;br /&gt;    doc_delete = Document.new()&lt;br /&gt;    doc_delete.add_element('files')&lt;br /&gt;    &lt;br /&gt;    ddoc.root.elements.each('file') do |file_node|&lt;br /&gt;      sfile = file_node.text&lt;br /&gt;      idate = Array.new&lt;br /&gt;      idate[0] = sfile.match(/^\d*/).to_s.to_i&lt;br /&gt;      idate[1] = sfile.match(/\_\d*/).to_s.gsub(separator,'').to_i&lt;br /&gt;      idate[2] = sfile.match(/\_\d*\d$/).to_s.gsub(separator,'').to_i&lt;br /&gt;&lt;br /&gt;      h = Hash.new&lt;br /&gt;      0.upto(2) {|i| h[a_format[i]] = idate[i]}&lt;br /&gt;&lt;br /&gt;      file_date = Date.new(y=h['y'], m=h['m'], d=h['d'])&lt;br /&gt;&lt;br /&gt;      if file_date &lt; cut_off_date&lt;br /&gt;        o_file2delete = Element.new('file')&lt;br /&gt;        o_file2delete.text = file_date.strftime(dformat)&lt;br /&gt;        doc_delete.root.add_element o_file2delete&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    file_delete.puts doc_delete&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: dir.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;dir&gt;&lt;br /&gt;  &lt;file&gt;11_4_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_5_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_6_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_7_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_8_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_9_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_10_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_11_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_1_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_12_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_13_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_14_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_15_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_16_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_17_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_18_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_19_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_20_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_21_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_2_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_22_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_23_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_24_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_25_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_26_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_27_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_28_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_3_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_4_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_5_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_6_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_7_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_8_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_9_2007&lt;/file&gt;&lt;br /&gt; &lt;/dir&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: files2delete.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;files&gt;&lt;br /&gt;  &lt;file&gt;11_4_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_5_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_6_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_7_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_8_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;11_9_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_10_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_11_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_1_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_12_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_13_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_14_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_15_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_2_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_3_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_4_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_5_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_6_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_7_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_8_2007&lt;/file&gt;&lt;br /&gt;  &lt;file&gt;12_9_2007&lt;/file&gt;&lt;br /&gt; &lt;/files&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Dec 2007 17:33:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4934</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Using Regular Expressions to validate a fixed length numerical string.</title>
      <link>http://snippets.dzone.com/posts/show/4627</link>
      <description>Using Ruby and Regular Sxpressions, this code successfully validates a string if it contains exactly 3 numbers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;a = '547'&lt;br /&gt;/^\d\d\d$/ ~= a&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Oct 2007 11:55:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4627</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Using Regular Expressions to validate a numerical string.</title>
      <link>http://snippets.dzone.com/posts/show/4626</link>
      <description>Using Ruby and Regular Expressions, this code validates a string for numbers only. If the string variable contains only numbers then 0 will be returned otherwise nil.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;a = '134323'&lt;br /&gt;/^[0-9]*$/ =~ a&lt;br /&gt;#result returns 0, indicating success&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Oct 2007 11:45:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4626</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Rails URL Validation</title>
      <link>http://snippets.dzone.com/posts/show/4532</link>
      <description>No regexes, allows URLs with ports or IPs. Inspiration from &lt;a href="http://actsasblog.wordpress.com/2006/10/16/url-validation-in-rubyrails/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_each :href, :on =&gt; :create do |record, attr, value|&lt;br /&gt;    begin&lt;br /&gt;      uri = URI.parse(value)&lt;br /&gt;      if uri.class != URI::HTTP&lt;br /&gt;        record.errors.add(attr, 'Only HTTP protocol addresses can be used')&lt;br /&gt;      end&lt;br /&gt;    rescue URI::InvalidURIError&lt;br /&gt;      record.errors.add(attr, 'The format of the url is not valid.')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 14:03:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4532</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>validate parent model </title>
      <link>http://snippets.dzone.com/posts/show/3990</link>
      <description>Validate a parent model in child model&lt;br /&gt;based on a post at http://www.railsweenie.com/forums/1/topics/439&lt;br /&gt;question : can this be improvised?&lt;br /&gt;&lt;br /&gt;Book (parent)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Book &lt; ActiveRecord::Base&lt;br /&gt;  has_many :pages&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Pages (children)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Pages &lt; ActiveRecord::Base&lt;br /&gt;  belongs_to :book&lt;br /&gt;  &lt;br /&gt;  validates_presence_of :page_id # this is not enough, anything can go in this field&lt;br /&gt;&lt;br /&gt;  # we check if the parent object is valid or not&lt;br /&gt;  validate do |page|&lt;br /&gt;   if page.blank?&lt;br /&gt;     unless page.book(true)&lt;br /&gt;       errors.add(:page_id, "must be a valid Book id")&lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 11 May 2007 01:22:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3990</guid>
      <author>edxerx (Ed Erx)</author>
    </item>
    <item>
      <title>Snazzy url fixer using Ruby's super method</title>
      <link>http://snippets.dzone.com/posts/show/3987</link>
      <description>Within the body of a method, a call to super acts just like a call to that original method, except that the search for a method body starts in the superclass of the object that was found to contain the original method.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def url=(addr)&lt;br /&gt;    super (addr.blank? || addr.starts_with?('http')) ? addr : "http://#{addr}"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 10 May 2007 22:46:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3987</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Validate uniqueness of an id pair</title>
      <link>http://snippets.dzone.com/posts/show/3970</link>
      <description>&lt;code&gt;&lt;br /&gt;# Prevent user from joining group twice&lt;br /&gt;def validate&lt;br /&gt;	errors.add_to_base("You are already a member of the #{self.group.name} Group") unless Grouping.find(:all, :conditions =&gt; {:user_id =&gt; self.user_id, :group_id =&gt; self.group_id}).blank?&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 May 2007 02:22:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3970</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Javascript numeric validation</title>
      <link>http://snippets.dzone.com/posts/show/3381</link>
      <description>I've seen a lot of takes on numeric validation, and most have serious flaws. For example, (!isNaN) returns inconsistent results and shouldn't be used to verify numericality. This should cover all cases, and supports a decimal point as well as negative numbers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function isNumeric(value) {&lt;br /&gt;  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This logic is straight-forward: if the parameter isn't null, convert it to a string and match it against a RegEx to throw out false cases. Otherwise, return true.</description>
      <pubDate>Mon, 29 Jan 2007 11:37:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3381</guid>
      <author>leftist (Pete Forde)</author>
    </item>
  </channel>
</rss>
