Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Custom Validator
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01057025
Message ID:
01058411
Views:
25
I tend to do all the validation I can on the aspx page and include a js file with all the validation. For example I need to validate that the form field has been entered,format is a date format, and that the age is between 18 and 60. I am able to stuff the control when the validator function on the client is called.
In the aspx page:
<input id="txtDOB" type="text" maxlength="10" style="WIDTH: 100px" name="txtDOB" runat="server">
<br>
<span style="FONT-SIZE: 7pt;FONT-FAMILY: arial">use mm/dd/yyyy as format<br>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="txtDOB" ErrorMessage="DOB is required.<br>" ID="Requiredfieldvalidator2"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ControlToValidate="txtDOB" ErrorMessage="DOB is required as mm/dd/yyyy.<br>" ID="RegEDOB" ValidationExpression="\d{2}\/\d{2}\/\d{4}"/>        
<asp:CustomValidator ID="cvDOBDate" dateBox="txtDOB" ErrorMessage="Age is not in range (18 - 90).<br>" ClientValidationFunction="ValidateAge" EnableClientScript=True Display="Dynamic" runat=server/>        
      </span>
And then this function on thye js include file:
////////////////////////////////////////////////////////
			function calcAgeYears( f ) { 
			  // Get entered values 
			   var now = new Date(); 
			   // Generate date from input 
			   var xD = new Date(f); 
			   // Check generated date is OK 
			   if ( isNaN(xD.getDate()) ) { 
				 return 'Entered text is not valid'; 
			   } 
			   // Get date components 
			   var  aY = xD.getFullYear(); 
			   var  aM = xD.getMonth(); 
			   var  aD = xD.getDate(); 
			   // Calculate age 
			   aY  = now.getFullYear() - aY; 
			   aM  = now.getMonth() - aM; 
			   aD  = now.getDate() - aD; 
			   if ( aD < 0) { 
				 aD = now.getDate( now.setDate(aD) ); 
				 aM--; 
			   } 
			   if ( aM < 0 ) { 
				 aM += 12; 
				 aY--; 
			   } 
			//   return aY + 'y ' + aM + 'm ' + aD + 'd'; 
			   return aY ; 
			} 
/////////////////////////////////////////////////////////
			function ValidateAge(source, arguments)
			{
			var comparisonToday= new Date();
			var todaysDate= new Date(document.getElementById(source.dateBox).value);
			var intDays;
			if(calcAgeYears(todaysDate) < 18 || calcAgeYears(todaysDate)>90 )
			{
				arguments.IsValid = false;
			}
			else
			{
				arguments.IsValid = true;
			}
			}
I just have a long js file with most of my validators...
Ricardo A. Parodi
eSolar, Inc.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform