I am giving the code for the fallowing…
- Validate the date format
- Not allowing to select previous date
- Not allowing to select future date
- Allowing only to enter date
- Allowing only phone numbers(US Format)
- Check for valid phone no or not (US Format)
- Allowing only zip code
- Check for valid zip code or not
- Allowing only SSN
- Check for valid SSN or not
- Allowing only Numbers
- Allowing only Alphabets
- Allowing only Alpha Numeric characters
- Allowing only special characters
- Allowing Only Decimal values
- Allowing only capital letters
- Allowing only small letters
- Trims trailing whitespace chars
- Trims leading whitespace chars
- Removes leading and trailing spaces
- Prevents event firing on enter key press
- Disabling BackSpace and delte button
how to Use this validations???
We have to Use this functions based on the situations, we can use textbox's "onkeypress()", "onblur()" events.
Here is a sample...
- <asp:TextBox ID="txtname" runat="server" onkeypress="isDateNumberKey(event,this)" onblur="return ValidateDateNoBack(this)"></asp:TextBox>
- <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtname" Format="MM/dd/yyyy">
- </cc1:CalendarExtender>
In the above code, I use two functions one for Not allowing the previous date and another for the
allow to enter only date. Here you can enter only date.
1.Validate the date format
- function ValidateDate(ctrl)
- {
- if (ctrl.value.length != 10)
- {
- if (ctrl.value.length == 0)
- {
- return true;
- }
- else
- {
- alert('Invalid Date! Please enter in MM/DD/YYYY or MM-DD-YYYY format.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- }
- if (ctrl.value.length > 0)
- {
- strValue = ctrl.value;
- var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
- //check to see if in correct format
- if(!objRegExp.test(strValue))
- {
- alert('Invalid Date! Please enter in MM/DD/YYYY or MM-DD-YYYY format.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false; //doesn't match pattern, bad date
- }
- else
- {
- var strSeparator = strValue.substring(2,3) //find date separator
- var arrayDate = strValue.split(strSeparator); //split date into month, day, year
- //create a lookup for months not equal to Feb.
- var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
- '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
- var intDay = parseInt(arrayDate[1],10);
- var intYear = parseInt(arrayDate[2]);
- if ((intYear < 1900) || (intYear > 2020))
- {
- alert('Invalid Date! Please enter a year between 1900 and 2020.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- //check if month value and day value agree
- if(arrayLookup[arrayDate[0]] != null)
- {
- if ((intDay <= arrayLookup[arrayDate[0]]) && (intDay > 0))
- {
- return true;
- }
- else
- {
- alert('Invalid Date! Please enter a valid Date.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- }
- var intMonth = parseInt(arrayDate[0],10);
- if ((intMonth < 0) ||(intMonth > 11))
- {
- alert('Invalid Month! Please enter a valid month.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- if (intMonth == 2)
- {
- var intYear = parseInt(arrayDate[2]);
- if (intDay > 0 && intDay < 29)
- {
- return true;
- }
- else if (intDay == 29)
- {
- if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0))
- {
- // year div by 4 and ((not div by 100) or div by 400) ->ok
- return true;
- }
- }
- else
- {
- alert('Invalid Date! Please enter a valid Date.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- }
- }
- alert('Invalid Date! Please enter in MM/DD/YYYY or MM-DD-YYYY format.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false; //any other values, bad date
- }
- else
- {
- return true;
- }
- }
2.Not allowing to select previous date
- //No Back date
- function ValidateDateNoBack(ctrl)
- {
- if (ctrl.value.length > 0 && ValidateDate(ctrl))
- {
- strValue = ctrl.value;
- //Check here to validate previous dates.
- var strSeparator2 = strValue.substring(2,3); //find date separator
- var arrayDate2 = strValue.split(strSeparator2); //split date into month, day, year
- var myDate=new Date();
- var yr1 = parseInt(arrayDate2[2]);
- var mon1 = parseInt(arrayDate2[0]-1,10);
- var day1 = parseInt(arrayDate2[1],10);
- myDate.setFullYear(yr1,mon1,day1);
- var today = new Date();
- if (myDate<today)
- {
- alert('Invalid Date! This field accepts only future dates. Please enter a future date');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- }
- else
- {
- return true;
- }
- }
3.Not allowing to select future date
- //No Future Date
- function ValidateDateNoFuture(ctrl)
- {
- if (ctrl.value.length > 0 && ValidateDate(ctrl))
- {
- strValue = ctrl.value;
- //Check here to validate previous dates.
- var strSeparator2 = strValue.substring(2,3); //find date separator
- var arrayDate2 = strValue.split(strSeparator2); //split date into month, day, year
- var myDate=new Date();
- var yr1 = parseInt(arrayDate2[2]);
- var mon1 = parseInt(arrayDate2[0]-1,10);
- var day1 = parseInt(arrayDate2[1],10);
- myDate.setFullYear(yr1,mon1,day1);
- var today = new Date();
- if (myDate>today)
- {
- alert('Invalid Date! This field does not accept a future date. Please enter a valid Date');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- }
- else
- {
- return true;
- }
- }
4.Allowing only to enter date
- //Alow only Enter to date
- function isDateNumberKey(evt,ctrl)
- {
- // Will allow '/' and numbers only.
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode == 47 || charCode == 45)
- return true;
- else
- {
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- if (ctrl.value.length == 2)
- {
- ctrl.value = ctrl.value + "/";
- }
- else if (ctrl.value.length == 5)
- {
- var idx = ctrl.value.indexOf("-");
- if (idx == -1)
- {
- ctrl.value = ctrl.value + "/";
- }
- else
- {
- ctrl.value = ctrl.value + "-";
- }
- }
- return true;
- }
- }
- }
5.Allowing only phone numbers(USA Format)
- //only phone numbers
- function isPhoneNumberKey(evt,ctrl)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode == 45)
- return true;
- else
- {
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- if (ctrl.value.length == 3)
- {
- ctrl.value = ctrl.value + "-";
- }
- else if (ctrl.value.length == 7)
- {
- ctrl.value = ctrl.value + "-";
- }
- return true;
- }
- }
- }
6.Check for valid phone no or not (USA Format)
- //Allow phone numbers format only(US Format)
- function ValidatePhone(ctrl)
- {
- var strvalue = ctrl.value;
- if (strvalue != "")
- {
- var objRegExp = /^(\d{3})-?\d{3}-?\d{4}$/;
- if (objRegExp.test(strvalue) == false)
- {
- alert('Invalid Home Phone number. Please use XXX-XXX-XXXX format.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- else if (strvalue.length == 10)
- {
- alert('Invalid Home Phone number. Please use XXX-XXX-XXXX format.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- return true;
- }
- else
- {
- return true;
- }
- }
7.Allowing only zip code(USA)
- //only zip code numbers
- function isZipNumberKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode == 45)
- return true;
- else
- {
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- return true;
- }
- }
8.Check for valid zip code or not(USA)
- //Zip Code
- function validateUSZip(ctrl)
- {
- var strValue = ctrl.value;
- var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
- if (objRegExp.test(strValue) == false)
- {
- alert('Invalid Zipcode.')
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- return true;
- }
9.Allowing only SSN(USA)
- //only zip code numbers
- function isZipNumberKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode == 45)
- return true;
- else
- {
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- return true;
- }
- }
10.Check for valid SSN or not(USA)
- //Allow SSN format only
- function ValidateSSN(ctrl)
- {
- if (ctrl.value.length > 0)
- {
- var strval = ctrl.value;
- if (strval.length != 11)
- {
- alert('Invalid SSN! Please use XXX-XX-XXXX format.');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- var data = strval.replace('-','');
- data = data.replace('-','');
- if (data.length != 9)
- {
- alert('Invalid SSN! Please use XXX-XX-XXXX format.');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- else if (data.indexOf("-") != -1)
- {
- alert('Invalid SSN! Please use XXX-XX-XXXX format.');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- var part1 = strval.charAt(3);
- var part2 = strval.charAt(6);
- if (part1 != '-')
- {
- alert('Invalid SSN! Please use XXX-XX-XXXX format.');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- if (part2 != '-')
- {
- alert('Invalid SSN! Please use XXX-XX-XXXX format.');
- ctrl.value = "";
- ctrl.focus();
- window.event.keyCode = 0;
- return false;
- }
- return true;
- }
- }
- }
- }
11.Allowing only Numbers
- //Number only
- function isOnlyNumberKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- return true;
- }
12.Allowing only Alphabets
- //Alphabets only
- function isOnlyAlphaKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (((charCode >= 65) && (charCode <= 90)) || ((charCode >= 97) && (charCode <= 122)) || (charCode == 32))
- return true;
- else
- {
- window.event.keyCode = 0;
- return false;
- }
- }
13.Allowing only Alpha Numeric characters
- //Alphanumerals only
- function isAlphaNumericKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (((charCode >= 48) && (charCode <= 57)) || (((charCode >= 65) && (charCode <= 90)) || ((charCode >= 97) && (charCode <= 122))))
- return true;
- else
- {
- window.event.keyCode = 0;
- return false;
- }
- }
14.Allowing only special characters
- //Only Special Characters
- function isSpecialCharsKey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (((charCode >= 48) && (charCode <= 57)) || (((charCode >= 65) && (charCode <= 90)) || ((charCode >= 97) && (charCode <= 122))))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- return true;
- }
15.Allowing Only Decimal values
- //Only Decimal values
- //onkeypress="return isKeyDecimal(this,event);"
- function isKeyDecimal(ctrl,evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if (charCode != 46 && (charCode < 48 || charCode > 57))
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- if (charCode == 46)
- {
- var abc = ctrl.value.split(".");
- if (abc.length > 1)
- {
- window.event.keyCode = 0;
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return true;
- }
- }
- }
16.Allowing Only Capital letters
- //ALLOW ONLY CAPITAL LETTERS
- function onlyUpperCase(ctrl)
- {
- ctrl.value = (ctrl.value.toUpperCase());
- }
17.Allowing Only small letters
- //ALLOW ONLY SMALL LETTERS
- function onlyLowerCase(ctrl)
- {
- ctrl.value = (ctrl.value.toLowerCase());
- }
18.Trims trailing whitespace chars
- //Trims trailing whitespace chars.
- //strValue - String to be trimmed.
- //returns - Source string with right whitespaces removed.
- function rightTrim( strValue )
- {
- var objRegExp = /^([\w\W]*)(\b\s*)$/;
- if(objRegExp.test(strValue)) {
- //remove trailing a whitespace characters
- strValue = strValue.replace(objRegExp, '$1');
- }
- return strValue;
- }
19.Trims leading whitespace chars
- //Trims leading whitespace chars.
- //strValue - String to be trimmed.
- //returns - Source string with left whitespaces removed.
- function leftTrim( strValue )
- {
- var objRegExp = /^(\s*)(\b[\w\W]*)$/;
- if(objRegExp.test(strValue)) {
- //remove leading a whitespace characters
- strValue = strValue.replace(objRegExp, '$2');
- }
- return strValue;
- }
20.Removes leading and trailing spaces
- //Removes leading and trailing spaces.
- //strValue - Source string to be trimmed.
- //returns - Source string with whitespaces removed.
- function trimAll( strValue ) {
- var objRegExp = /^(\s*)$/;
- //check for all spaces
- if(objRegExp.test(strValue)) {
- strValue = strValue.replace(objRegExp, '');
- if( strValue.length == 0)
- return strValue;
- }
- //check for leading & trailing spaces
- objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
- if(objRegExp.test(strValue)) {
- //remove leading and trailing whitespace characters
- strValue = strValue.replace(objRegExp, '$2');
- }
- return strValue;
- }
21.Prevents event firing on enter key press
- //Prevents event firing on enter key press
- function AvoidEnterKeyEvent()
- {
- if (window.event.keyCode == 13)
- {
- event.returnValue=false;
- event.cancel = true;
- }
- }
22.Disabling BackSpace and delete button
- function funkey(evt)
- {
- var charCode = (evt.which) ? evt.which : event.keyCode;
- if((charCode == 8) || (charCode == 46))
- {
- return false;
- }
- else
- return true;
- }
3 comments:
nice work
ASP useful tips
http://aspusefultips.blogspot.com/
Thanks
Post a Comment