﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Validation</title>
	<atom:link href="http://byatool.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>ANDRE SMASH!!!!</description>
	<lastBuildDate>Wed, 28 Jul 2010 18:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery Validation: Adding Errors to the Error Containter&#8230; With Love!</title>
		<link>http://byatool.com/lessons/jquery-validation-adding-errors-to-the-error-containter-with-love/</link>
		<comments>http://byatool.com/lessons/jquery-validation-adding-errors-to-the-error-containter-with-love/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 21:28:49 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1178</guid>
		<description><![CDATA[Hello again, my wonderful audience, I have something great and fun planned for this post. If you read this post you might have been left wondering how to add custom messages to the error holder. It's ok if you did, there's nothing wrong with being confused all the time. It's what makes you so adorable. [...]]]></description>
			<content:encoded><![CDATA[<pre><img src="http://www.iheartvector.com/wp-content/vector/hearts.png" alt="" /></pre>
<p><span style="background-color: #FFFFFF;" title="Please send help, the Board of Directors is forcing me to be nice!">Hello again, my wonderful audience, I have something great and fun planned for this post. If you read <a href="http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/">this post</a> you might have been left wondering how to add custom messages to the error holder. It's ok if you did, there's nothing wrong with being confused all the time. It's what makes you so adorable.</span></p>
<p><span style="background-color: #FFFFFF;" title="I really don't mean the last part.">Say you have a form like this, and if you don't I cry endlessly for your loss. My heart goes out to you.</span></p>
<pre>&lt;<span style="color: #800000;">form</span> <span style="color: #0000ff;">id=</span><span style="color: #ff0000;">"ILoveEverythingForm"</span>&gt;
    &lt;<span style="color: #800000;">div</span> <span style="color: #0000ff;">id=</span><span style="color: #ff0000;">"NotSoNiceThingsDiv"</span> <span style="color: #0000ff;">style</span><span style="color: #0000ff;">=</span><span style="color: #ff0000;">"display:none;"</span>&gt;&lt;/<span style="color: #800000;">div</span>&gt;
	...
&lt;/<span style="color: #800000;">form</span>&gt;</pre>
<p><span style="background-color: #FFFFFF;" title="This is the cheesiest piece of...">As you can see, there is an error div to hold mean things that we have to print out because the system just doesn't like the user. As we both know, the user can't help but be dumb. It's just in his cute little nature. And that nature is just so darned cute.</span></p>
<p><span style="background-color: #FFFFFF;" title="Uhg, this really sucks.">Now let's assume that you have something that sent back a bunch of mean messages when you tried to validate server side after all the cute client side validation was done. You might have a method to take in those jerk face messages. How do you display them in the bad place?</span></p>
<pre>  <span style="color: #0000ff;">function</span> updateErrorDivContentWithLove(messages)
  {
    <span style="color: #0000ff;">if</span>(messages.length &gt; 0)
    {
      <span style="color: #0000ff;">var</span> validator = jQuery(<span style="color: #800000;">'#ILoveEverythingForm'</span>).validate();

      <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; messages.length; loopCounter ++)
      {
        validator.showErrors
        (
          {
             <span style="color: #800000;">'SomeWonderfulElementName'</span> : messages[loopCounter].Message
          }
        );
      }
    }
  }</pre>
<p><span style="background-color: #FFFFFF;" title="I wish I could stab my eyes right now...">Awwww kitties!</span></p>
<pre>  <img src="http://www.dailypets.co.uk/wp-content/uploads/2007/06/kittens-cups.jpg" alt="" /></pre>
<p><span style="background-color: #FFFFFF;" title="I am losing mad rep on this.">Only thing that may seem odd (And if it doesn't, don't worry you are still very special and loved in this world):</span></p>
<pre>  'SomeElementName' : messages[loopCounter].Message</pre>
<p><span style="background-color: #FFFFFF;" title="Oh for f--ks sake...">Not sure what element really needs to be here, basically anything in the form with a name tag. So if you're good like I know you are, you have at least one element in that little old form with a name property set. I would say that this is pretty useless in situations where all the errors are shown in one div, as opposed to right next to the element, but that would be mean and I don't do that.</span></p>
<p><span style="background-color: #FFFFFF;" title="I swear I'm not really a nice person, please believe me.">Now I know you wanted something special from this post so I just wanted you to know that even though most of the world considers you hideous, I say you should be proud of being hideous because it makes you who you are. A unique miracle that only really has issues with dogs and small children. Embrace yourself in whatever way you take that!</span></p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/" title="jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains">jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains</a></li><li><a href="http://byatool.com/writing/byatool-com-gets-a-shiny-new-tool/" title="ByATool.com gets a shiny new tool!">ByATool.com gets a shiny new tool!</a></li><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/get-the-jquery-datepicker-to-work-with-jquery-modal-dialog/" title="Get the jQuery DatePicker to Work With jQuery Modal Dialog">Get the jQuery DatePicker to Work With jQuery Modal Dialog</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/jquery-validation-adding-errors-to-the-error-containter-with-love/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Data Annotations With MVC: How to Check Multiple Properties at One Time</title>
		<link>http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/</link>
		<comments>http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 22:54:35 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[Data Annotations]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1162</guid>
		<description><![CDATA[Ok so maybe you read this post and you're thinking to yourself, "I wonder if Diet Dr. Pepper really does taste like regular Dr. Pepper" which of course is silly because the ads clearly say it does. Now you should be asking yourself, "How do I check if two properties have the same value on [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so maybe you read <a href="http://byatool.com/lessons/data-annotations-mvc-and-why-you-might-like-them/">this post</a> and you're thinking to yourself, "I wonder if Diet Dr. Pepper really does taste like regular Dr. Pepper" which of course is silly because the ads clearly say it does. Now you should be asking yourself, "How do I check if two properties have the same value on a class with annotation when the property level ones only can check the one property's value?" Kind of wordy way of asking that, but I get what you're asking. Better yet, like usual, I have an answer.</p>
<p>Now what I can swear is that I was no where near the Baxter Building between 1-3 am on Saturday the 15th. I was in fact I was sleeping and have 20 witnesses that can testify. What I can't swear is this is the best way to go about it but it seems to work AND make sense. It's not common for me to come up with solutions that do both.</p>
<pre>  [<span style="color: #008080;">AttributeUsage</span>(<span style="color: #008080;">AttributeTargets</span>.Class)]
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">PropertiesMatchAttribute </span>: <span style="color: #008080;">ValidationAttribute</span>
  { 

    <span style="color: #0000ff;">public</span> <span style="color: #008080;">String</span> FirstPropertyName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    <span style="color: #0000ff;">public</span> <span style="color: #008080;">String</span> SecondPropertyName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } 

    <span style="color: #008080;">/<span style="color: #008000;">/Constructor to take in the property names that are supposed to be checked</span></span>
    <span style="color: #0000ff;">public</span> PropertiesMatchAttribute(<span style="color: #008080;">String</span> firstPropertyName, <span style="color: #008080;">String</span> secondPropertyName )
    {
      FirstPropertyName = firstPropertyName;
      SecondPropertyName = secondPropertyName ;
    } 

    <span style="color: #0000ff;">p</span><span style="color: #0000ff;">ublic</span> <span style="color: #0000ff;">override</span> <span style="color: #008080;">Boolean</span> IsValid(<span style="color: #008080;">Object</span> value)
    {
      <span style="color: #008080;">Type</span> objectType = value.GetType();
      <span style="color: #008000;">//Get the property info for the object passed in.  This is the class the attribute is</span>
      <span style="color: #008000;">//  attached to</span>
      <span style="color: #008000;">//I would suggest caching this part... at least the PropertyInfo[]</span>
      <span style="color: #008080;">PropertyInfo</span>[] neededProperties =
        objectType.GetProperties()
        .Where(propertyInfo =&gt; propertyInfo.Name == FirstPropertyName || propertyInfo.Name == SecondPropertyName)
        .ToArray();

      <span style="color: #0000ff;">if</span>(neededProperties.Count() != 2)
      {
        <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #008080;">ApplicationException</span>("PropertiesMatchAttribute error on " + objectType.Name);
      }

      <span style="color: #008080;">Boolean</span> isValid = <span style="color: #0000ff;">true</span>;

      <span style="color: #008000;">//Convert both values to string and compare...  Probably could be done better than this</span>
      <span style="color: #008000;">//  but let's not get bogged down with how dumb I am.  We should be concerned about</span>
      <span style="color: #008000;">//  dumb you are, jerkface.</span>
      <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">Convert</span>.ToString(neededProperties[0].GetValue(value, <span style="color: #0000ff;">null</span>)).Equals(<span style="color: #008080;">Convert</span>.ToString(neededProperties[1].GetValue(value, <span style="color: #0000ff;">null</span>))))
      {
        isValid = <span style="color: #0000ff;">false</span>;
      }

      <span style="color: #0000ff;">r</span><span style="color: #0000ff;">eturn</span> isValid;
    }
  }
}</pre>
<p>And then the use:</p>
<pre>  [<span style="color: #008080;">PropertiesMatchAttribute</span>(<span style="color: #800000;">"Password"</span>, <span style="color: #800000;">"ConfirmPassword"</span>, ErrorMessage = <span style="color: #800000;">"Match the passwords, dumb--s."</span>)]
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">UserCreateModel</span>
  {
     <span style="color: #0000ff;">public</span> <span style="color: #008080;">String</span> ConfirmPassword{ <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
     <span style="color: #0000ff;">public</span> <span style="color: #008080;">String</span> Password { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
  }</pre>
<p>Wasn't so hard was it? Normally this would be the part where I degrade you for being bumb because you didn't figure this out but I'm above that now. I guess it must be the holiday season or that I've been told by the board that I should be more gentle with my readers. Guess some were crying after reading some of my posts. Yeah whatever. Bunch of [Removed by Andre].</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/lessons/jquery-validation-adding-errors-to-the-error-containter-with-love/" title="jQuery Validation: Adding Errors to the Error Containter&#8230; With Love!">jQuery Validation: Adding Errors to the Error Containter&#8230; With Love!</a></li><li><a href="http://byatool.com/lessons/data-annotations-mvc-and-why-you-might-like-them/" title="Data Annotations, MVC, and Why You Might Like Them">Data Annotations, MVC, and Why You Might Like Them</a></li><li><a href="http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/" title="jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains">jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>jQuery Validation &#8211; How to Use to Get Rid Of Even The Toughest Stains</title>
		<link>http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/</link>
		<comments>http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 18:17:29 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1092</guid>
		<description><![CDATA[So you want to use jQuery validation, huh? What is it? Something that was added to the holy jquery site and is an easy way to validate input from users. Now this should in no way take over for server side validation, but it helps to at least catch a few things without having to [...]]]></description>
			<content:encoded><![CDATA[<p>So you want to use jQuery validation, huh?</p>
<p>What is it?  Something that was added to the<a href="http://jquery.com/"> <strong>holy jquery site</strong></a> and is an easy way to validate input from users. Now this should in no way take over for server side validation, but it helps to at least catch a few things without having to send anything to the server.  So how do ya do it?</p>
<p>Well to start, you need some files:</p>
<p><a href="http://docs.jquery.com/Release:jQuery_1.3.2">jquery-1.3.2.js</a> and <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jquery.validate.js</a>.</p>
<p>Now oddly enough the validation file isn't hosted on the <a href="http://jquery.com/"> <strong>holy jquery site</strong></a> but <a href="http://docs.jquery.com/Plugins/Validation">how to use it is</a>.</p>
<p>Ok now you have the files, what's next?  Well you need form, and <a href="http://byatool.com/hosted/LiveDemos/jQueryValidation/SimpleRequired.html">I can do that for you.</a></p>
<p>So basically it's a simple form with one input that is required.</p>
<pre>jQuery(document).ready
    (
      <span style="color: #0000ff;">function</span>()
      {
        jQuery(<span style="color: #800000;">"#PrimaryForm"</span>).validate
        (
          {
            errorLabelContainer: <span style="color: #800000;">"#ErrorDiv"</span>,
            wrapper: <span style="color: #800000;">"div"</span>,
            rules:
            {
              FirstName :
              {
                required : <span style="color: #0000ff;">true</span>
              }
            },
            messages:
            {
              FirstName:
              {
                required : <span style="color: #800000;">'First Name is required.'</span>
              }
            },
            onfocusout : false,
            onkeyup: false,
            submitHandler: <span style="color: #0000ff;">function</span>(label)
            {
              postSubmit();
            }
          }
        );
      }</pre>
<pre>jQuery(<span style="color: #800000;">"#PrimaryForm"</span>).validate</pre>
<p>Real simple, just setting the validator to the primary form on the page.</p>
<pre> errorLabelContainer: <span style="color: #800000;">"#ErrorDiv"</span>,</pre>
<p>This sets the errors to show up in the ErrorDiv.  Now this is optional, as you can have it show the errors next to the FirstName text box but personally I think that looks horrible.  Setting up the ErrorDiv puts all the errors in one central location and allows for styling the actual div.</p>
<pre> rules:
 {
    FirstName :
    {
      required : <span style="color: #0000ff;">true</span>
    }
  },</pre>
<p>This matches an element with the id of FirstName to the required rule, meaning that FirstName is required.  Rocket science.</p>
<pre>  messages:
  {
    FirstName:
    {
       required : <span style="color: #800000;">'First Name is required.'</span>
    }
  },</pre>
<p>If you can't figure this out, I hear circus is hiring for the "World's Dumbest Person".  You'll fit in with Jub Jub the Dog Boy.</p>
<pre>  onfocusout : <span style="color: #0000ff;">false</span>,
  onkeyup: <span style="color: #0000ff;">false</span>,</pre>
<p>Basically this prevents the validation when leaving the textbox or on every key press.  This is just another preference.</p>
<pre>  submitHandler: <span style="color: #0000ff;">function</span>(label)
  {
    postSubmit();
  }</pre>
<p>If the submit is successful, call this method.</p>
<p><strong>But... BUT WHAT IF IT'S AN EMAIL?!??! WHAT WILL I DO???!?!?</strong></p>
<p>Well for one, stop being such a child.  <a href="http://byatool.com/hosted/LiveDemos/jQueryValidation/EmailRequired.html">And two, look here.</a></p>
<p>Some what different, as you can see it's now email and there is one extra requirement in the rules:</p>
<pre>  rules:
  {
    EmailAddress :
    {
      email : <span style="color: #0000ff;">true</span>,
      required : <span style="color: #0000ff;">true</span>
    }
  },
  messages:
  {
    EmailAddress:
    {
      required : <span style="color: #800000;">'Yo, email fool.'</span>,
      email : <span style="color: #800000;">'So not an email address.'</span>
    },
  },</pre>
<p>See?  It has nice built in rule for email.  Simple.</p>
<p><strong>BUT WHAT IF I NEED A REGULAR EXPRESSION?!??! WHAT WILL I DO???!?!?</strong></p>
<p>I swear if you don't stop that, I'm turning this post around and going home.</p>
<p><a href="http://byatool.com/hosted/LiveDemos/jQueryValidation/ZipCodeRequired.html">Fine here it is.</a></p>
<pre>  jQuery.validator.addMethod
  (
    <span style="color: #800000;">"isZipCode"</span>,
    <span style="color: #0000ff;">function</span>(value, element)
    {
      <span style="color: #0000ff;">return</span> value.match(<span style="color: #0000ff;">new</span> RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/));
    }
  );</pre>
<p>Just have to create a method and "add it" to the validator itself. And then there's the use:</p>
<pre>  rules:
  {
    ZipCode :
    {
      required : <span style="color: #0000ff;">true</span>,
      isZipCode : <span style="color: #0000ff;">true</span>
    }
  },
  messages:
  {
    ZipCode:
    {
      required : <span style="color: #800000;">'For the love of me, enter a zip code!.'</span>,
      isZipCode : <span style="color: #800000;">'Serioulsy?  Do you know what a zip code is?'</span>
    },
  },</pre>
<p>Woo hoo right?</p>
<p>Don't do it... Don't you yell.</p>
<p><strong>But what if one input depends on another?</strong></p>
<p>Much better.  Well that's not as hard as it may seem and <a href="http://byatool.com/hosted/LiveDemos/jQueryValidation/TwoRequired.html">here's the example</a>.</p>
<pre>  rules:
  {
    InputB :
    {
      required :
      {
        depends : <span style="color: #0000ff;">function</span>(element) { <span style="color: #0000ff;">return</span> jQuery(<span style="color: #800000;">'#InputA'</span>).val() != <span style="color: #800000;">""</span> }
      }
    }
  },</pre>
<p>As you can see, you can change how the required method works by adding in a depends handler.  Works out pretty well.</p>
<p>Yes I will show you how to make sure two inputs match.  <a href="http://byatool.com/hosted/LiveDemos/jQueryValidation/EqualTo.html">I swear you ask for a lot.</a></p>
<pre>  rules:
  {
    Password :
    {
      equalTo : <span style="color: #800000;">"#ConfirmPassword"
</span>    },
  },</pre>
<p>Couldn't be easier unless I wrote it out for you.  Wait, I did.</p>
<p>So here you've either learned a bit about jQuery validation or have just spent the last few minutes drooling uncontrollably. Either way, I'm done with this post and you're left to do whatever it is you do, you sick f---.</p>
<p>Side note:  I haven't actually been to <a href="http://www.htmlgoodies.com">Htmlgoodies</a> since eh college?  but wow did that place sell out.  How fitting that an introduction to html page now looks like it was designed by someone just starting out... in the 90s.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/lessons/child-iframe-page-interacting-with-parent-page-yeah-i-went-there/" title="Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.">Child IFrame Page Interacting with Parent Page&#8230; Yeah I went there.</a></li><li><a href="http://byatool.com/lessons/jquery-validation-adding-errors-to-the-error-containter-with-love/" title="jQuery Validation: Adding Errors to the Error Containter&#8230; With Love!">jQuery Validation: Adding Errors to the Error Containter&#8230; With Love!</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li><li><a href="http://byatool.com/ui/jquery-position-absolute-and-how-to-make-it-all-work/" title="JQuery, Position : Absolute, and How to Make It All Work">JQuery, Position : Absolute, and How to Make It All Work</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/jquery-validation-how-to-use-to-get-rid-of-even-the-toughest-stains/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
