Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Sunday, January 16, 2011

Date validation in Infopath forms

In SharePoint infopath forms I need to implement one thing which was a bit tricky as it is not the default supported by infopath forms. The requirement is like this: The date fields on form should not allow date plus/minus 5 years from current server date. For this, so many people recommended to implement validation events in c#. But, my form contains 40+ date fields. That is not possible to implement events for each control. But, when I looked into all option there is an option called “Data Validation”. But, there is no direct option available to implement this requirement.
After thought about sometime got wonderful ideas. Below is the implementation finally came up with.
  1. Select the date control and right click.
  2. Select the option “data validation”.
  3. Click on Add to add a new validation.
  4. From the window, the first thing we have to check is “Whether the field is blank or not.” Because once it is not blank then only we go further.
  5. And next thing is, validate the expression. User entered date is plus/minus 5 years from current date.
image
If you observe the first condition I have used is an expression as there is no direct way to validate the date according to my requirement.
Expression we have to use: msxsl:string-compare(., xdDate:AddDays(xdDate:Today(), 1825)) > 0 or msxsl:string-compare(., xdDate:AddDays(xdDate:Today(), -1825)) < 0
And second condition is just the field is not blank then only display “Invalid date” error on screen.
Note: Remember, the expression is very simple that adding/subtracting the 1825 days [5 years] to current date. Depends on your requirement please change the value accordingly.
The last reason why I have used the expression instead of the simple statements is the logical operators. The condition should be validated for this requirement is “((selected date > today + 5 years OR selected date < today – 5years) AND field cannot be blank)”. The brackets are very important, the sequence of executing conditions and logical operators are important. So, to achieve this in infopath forms the only way is implementing expressions. Then only it treats it as the way we want. Enjoy the complex and nice series of posts on infopath and sharepoint coding.

Sunday, October 4, 2009

Client side validation of ASPX validation controls

This is the nice post under validating the ASPX validations in client side like java script. The concept behind is,

  • Some times we need to validate the .NET controls in client side whether they have correct values or not. We can do them in client side logic by using some existing functions available in javascript which are handled by ASP.NET framework.
  • And another requirement is on the page some controls are .NET controls and some are HTML controls. And all .NET controls are using the server side validation controls and html controls are using client side validation. So, when click on submit button, you need to detect whether form is valid or not on client side. So, you need to write some custom logic to detect all HTML controls are valid or not and then you need to detect all ASP.NET controls are having valid values or not. How will you detect that? You should validate both of them in javascript. So, this logic will help you to find the form is valid or not. 
function Page_ClientValidate(validationGroup) {
   Page_InvalidControlToBeFocused = null;
   if (typeof(Page_Validators) == "undefined") {
      return true;
   }
   var i;
   for (i = 0; i < Page_Validators.length; i++) {
      ValidatorValidate(Page_Validators[i], validationGroup, null);
   }
   ValidatorUpdateIsValid();
   ValidationSummaryOnSubmit(validationGroup);
   Page_BlockSubmit = !Page_IsValid;
   return Page_IsValid;
}

The above function is taking an argument named validationGroup. This is the validation group of the server side validation control. And the output or return value returning is the boolean value. If it is returning true then it passes the server side validation and false then it fails the server side validation. So, along with it's value you can write your own logic to validate the html controls and test whether form is valid or not.

Hope this helps and you can solve so many problems with this logic. Always welcome your valuable feedback and comments. Do you know any other ways to implement it?

Thursday, September 3, 2009

Jquery datepicker problem on date select in IE - 'length' is null or not an object

I am working on a project which includes the technologies like ASP.NET, Jquery, Sql server. I solved so many problems in Jquery and posted on my blog some. Now, I am posting a wonderful post on Jquery datepicker.
Because I am using Jquery in ASP.NET application, I added a regular expression for date to validate whether user entered date is valid or not. Because of adding the validation on the text box, whenever I select a date from datepicker, the datepicker events are firing like onselect, change etc.. So, generally what validation framework is doing, whenever some event fires on the page, validations will execute. The same thing happening here. Whenever I change or select date from the date picker, it will fire some events and at the same time validation framework trying to validate the validations. But, it is not the right event to do validations. So, always we will get the exception at "vals.length" in a for loop of the built-in code. vals is the object of all the validators on the page. It always fails to load in Jquery date picker event trigger, because it is not the right event for validation. And the result is vals is undefined, you always get exception at vals.length as "length is null or not an object". I think now you got very clear idea of why it is happening. Now, move to the next step i.e. solution for it.

Solution is very simple, I read all the documentation of Jquery datepicker and found an event named onSelect. So, whenever I select date this is the event firing. So, there I got a clue and started thinking towards it. And below is the result.

$(".datepicker").datepicker({ onSelect:function(){}});

In your datepicker initialization statement add onSelect event which don't do anything means empty function as shown above. There problem solved. Isn't a good find? Please let me know your thoughts on this.