Monday, July 7, 2008

InsertSpaceInString JS

function InsertSpaceInString(textWithoutSpace,size,controlName)
{
var controlToBind = document.getElementById(controlName);
var textWithSpace = "";
var temp = textWithoutSpace.split(" ");
//alert(textWithoutSpace);
for (var i = 0; i < spliter = "" remaining = ""> size)
{
var loopCount = temp[i].length / size;
remaining = temp[i];
for (var j = 1; j <= loopCount; j++) { var a = remaining.substring(0, size); remaining = remaining.substring((size)); spliter = spliter + a + " "; } temp[i] = spliter + remaining; } textWithSpace = " " + textWithSpace + temp[i] + " "; } if (textWithSpace.length > 0)
{
var temperorary = textWithSpace.trim();
textWithSpace = temperorary;
}
controlToBind.innerHTML = textWithSpace;
}

ValidTime JS

function isValidTime(timeStr, timeValue)
{
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AMamPMpm))?$/;
var matchArray = timeStr.match(timePat);
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];
if (matchArray == null)
{
alert("Time is not in a valid format.");
timeValue.select();
timeValue.focus();
return false;
}
if (second=="")
{
second = null;
}
if (ampm=="")
{
ampm = null
}
if (hour <> 23)
{
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
timeValue.select();
timeValue.focus();
return false;
}
if (hour <= 12 && ampm == null)
{
if (confirm("Please indicate which time format you are using. OK = Standard Time, CANCEL = Military Time"))
{
alert("You must specify AM or PM.");
timeValue.select();
timeValue.focus();
return false;
}
}
if (hour > 12 && ampm != null)
{
alert("You can't specify AM or PM for military time.");
timeValue.select();
timeValue.focus();
return false;
}
if (minute<0> 59)
{
alert ("Minute must be between 0 and 59.");
timeValue.select();
timeValue.focus();
return false;
}
if (second != null && (second <> 59))
{
alert ("Second must be between 0 and 59.");
timeValue.select();
timeValue.focus();
return false;
}
return true;
}

Time Validation JS

//Time Validation
/* This function validate time in the format of HH:MM:SS AP/PM. The argument timeValue is the id of the textbox.
The argument required defines whether this field is either optionsl or not. It allows time in two formats.
One is Railway time format(does not contain AM or PM) and another is normal. */
function validateTime(timeValue, required)
{
var timeStr = timeValue.value;
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AMamPMpm))?$/;
if(required == true)
{
if(timeStr == '' timeStr == "" timeStr == null)
{
alert("Enter the time");
timeValue.select();
timeValue.focus();
return false;
}
else
{
if(timeStr.match(timePat))
{
var s = isValidTime(timeStr, timeValue)
return s;
}
else
{
alert("Enter the time in the correct format");
timeValue.select();
timeValue.focus();
return false;
}
}
}
else
{
if(timeStr == '' timeStr == "" timeStr == null)
{
return true;
}
else
{
if(timeStr.match(timePat))
{
var s = isValidTime(timeStr, timeValue)
return s;
}
else
{
alert("Enter the time in the correct format");
timeValue.select();
timeValue.focus();
return false;
}
}
}
}

Allows Symbols JS

//This Allows Symbols only
/*The argument strText is the id of the textbox. This allows only symbols without text or numbers.*/
function symbolOnly(strText)
{
var str = strText.value;
//var format = /[^a-zA-Z0-9]+$/;
var format = /^[^0-9a-zA-Z]+$/;
if(str == "" str == null str == '')
{
alert("Enter any symbols");
strText.focus();
strText.select();
return false;
}
else
{
if(str.match(format))
{
return true;
}
else
{
alert("Enter Only Symbols");
strText.select();
strText.focus();
return false;
}
}
}

Only texts are allowed (a to z or A-Z). JS

/* Only texts are allowed (a to z or A-Z). The argument strValue is the id of the textbox.The argument required
defines the field is either optional or not. It takes the value of either true or false. The argument errorSpanID
is the id of the errorSpan
*/
function textOnly(controlID, required, errorSpanID)
{
var strValue = document.getElementById(controlID);
var errorSpan = document.getElementById(errorSpanID);
errorSpan.innerHTML = "";
var str = strValue.value;
var format = /^[A-Za-z]+$/;
if(required == true)
{
if(str == '' str == null str == "")
{
errorSpan.innerHTML = "Enter the Text";
strValue.focus();
strValue.select();
return false;
}
else
{
if (str.match(format))
{
return true;
}
else
{
errorSpan.innerHTML = "Invalid Text";
strValue.focus();
strValue.select();
return false;
}
}
}
else if(required == false)
{
if(str== '' str == null str == "")
{
return true;
}
else
{
if(str.match(format))
{
return true;
}
else
{
errorSpan.innerHTML = "Invalid Text";
strValue.focus();
strValue.select();
return false;
}
}
}
}

checkInternationalPhone,stripCharsInBag,isInteger JS

function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (((c < "0") (c > "9"))) return false;
}
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (bag.indexOf(c) == -1)
returnString += c;
}
return returnString;
}
function checkInternationalPhone(strPhone)
{
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= 10);
}

Phone number validation js

// Phone number validation
/*It validates the phone number in international level. The argument PhoenNumber is the id of textbox. PhoneNumber
must contain a minimum of 10 digits. checkRequired defines that it is either optional or must. */
function ValidatePhoneNumber(PhoneNumber, checkRequired)
{
var Phone = PhoneNumber.value;
if(checkRequired == true)
{
if (Phone == null Phone == "" Phone == '')
{
alert("Please Enter your Phone Number")
PhoneNumber.select();
PhoneNumber.focus();
return false;
}
}
else
{
if(Phone == null Phone == '' Phone == "")
{
return true;
}
}
if (checkInternationalPhone(Phone) == false)
{
alert("Please Enter a Valid Phone Number");
PhoneNumber.select();
PhoneNumber.focus();
return false;
}
return true;
}

Date Validation JS

//Date Validation
/*It validate the date in MM/DD/YYYY format. the argument DateValue is the id of the textbox. required defines that
this field is to be filled not not. it takes two value. one is true (Must filled). false means optional.*/
function validateDate(DateValue, required)
{
var date = DateValue.value;
var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]1[02])(\/-\.)31)\1(?:(?:0?[1,3-9]1[0-2])(\/-\.)(?:2930)\2))(?:(?:1[6-9][2-9]\d)?\d{2})(?:0?2(\/-\.)29\3(?:(?:(?:1[6-9][2-9]\d)?(?:0[48][2468][048][13579][26])(?:(?:16[2468][048][3579][26])00))))(?:(?:0?[1-9])(?:1[0-2]))(\/-\.)(?:0?[1-9]1\d2[0-8])\4(?:(?:1[6-9][2-9]\d)?\d{2}))($\ (?=\d)))?(((0?[1-9]1[012])(:[0-5]\d){0,2}(\ [AP]M))([01]\d2[0-3])(:[0-5]\d){1,2})?$/;
if(required == true)
{
if(date == '' date == null date == "")
{
alert("Enter the Date");
DateValue.select();
DateValue.focus();
return false;
}
else
{
if (date.match(RegExPattern))
{
return true;
}
else
{
alert("Enter a valid date as mm/dd/yyyy");
DateValue.select();
DateValue.focus();
return false;
}
}
}
else
{
if(date == '' date == null date == "")
{
return true;
}
else
{
if (date.match(RegExPattern))
{
return true;
}
else
{
alert("Enter a valid date as mm/dd/yyyy");
DateValue.select();
DateValue.focus();
return false;
}
}
}
}

Check the Confirm Passwordfunction JS

//Check the Confirm Passwordfunction
ValidateConfirmPassword(text1,text2,spanID){ document.getElementById(spanID).innerHTML= ''; textElement1=document.getElementById(text1); textElement2=document.getElementById(text2); var txt1 = textElement1.value; var txt2 = textElement2.value; if ( txt1 != txt2) { document.getElementById(spanID).innerHTML= 'Verify your password again'; return false; } else { return true; }}

check the length of Text or Numbers JS

//check the length of Text or Numbers
function ValidateLength(txtText,minSize,maxSize,required,spanID,controlName)
{
txtElement=document.getElementById(txtText);
var txt = txtElement.value;
document.getElementById(spanID).innerHTML='';
if ( required == true)
{
if(txt == '' txt == null txt == "")
{
document.getElementById(spanID).innerHTML=controlName+" is required";
return false;
}
else
{
if ( minSize > 0)
{
if(txt.length < minSize)
{
document.getElementById(spanID).innerHTML= 'Minimum is ' + minSize+ ' characters';
return false;
}
else
{
if (maxSize > 0)
{
if(txt.length > maxSize)
{
document.getElementById(spanID).innerHTML= 'Maximum is ' + maxSize+ ' characters';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
else
{
if (maxSize > 0)
{
if(txt.length > maxSize)
{
document.getElementById(spanID).innerHTML= 'Maximum is ' + maxSize+ ' characters';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
}
else
{
if ( minSize > 0)
{
if(txt.length < minSize)
{
document.getElementById(spanID).innerHTML= 'Minimum is ' + minSize+ ' characters';
return false;
}
else
{
if (maxSize > 0)
{
if(txt.length > maxSize)
{
document.getElementById(spanID).innerHTML= 'Maximum is ' + maxSize+ ' characters';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
else
{
if (maxSize > 0)
{
if(txt.length > maxSize)
{
document.getElementById(spanID).innerHTML= 'Maximum is ' + maxSize+ ' characters';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
}

Validate Text JS

//Validate Text
/*Only Alpha Numeric characters are allowed. The argument txtText is the id of textbox. checkRequired means the field is necessary or not
(necessary maens true otherwise false). The argument checkLength means to check the length of the
text against the size. We must send the size if we send checkLength as true otherwise not necessary*/
function ValidateText(txtText, checkRequired, checkLength, size,spanID,controlName)
{
txtElement=document.getElementById(txtText);
var txt = txtElement.value;
var format = /^[A-Za-z0-9\s]+$/;
document.getElementById(spanID).innerHTML='';
if(checkRequired == true)
{
if(txt == "" txt == '' txt == null)
{
document.getElementById(spanID).innerHTML=controlName+' is required';
return false;
}
else
{
if(txt.match(format))
{
if(checkLength == true)
{
if(txt.length > size)
{
document.getElementById(spanID).innerHTML='Maximum is '+ size +' characters.';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML='Invalid Characters';
return false;
}
}
}
else
{
if(txt == "" txt == '' txt == null)
{
return true;
}
else
{
if(txt.match(format))
{
if(checkLength == true)
{
if(txt.length > size)
{
document.getElementById(spanID).innerHTML= 'Maximum is '+ size +' characters.';
return false;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML= 'Invalid Characters';
return false;
}
}
}
}

Validate Number JS

//Validate Number
/*Only Numbers are allowed. The argument number is the id of textbox. checkRequired means the field is necessary or not
(necessary maens true otherwise false). The argument checkLength means to check the length of the
number against the size. We must send the size if we send checkLength as true otherwise not necessary*/
function ValidateNumber(textElement, checkRequired, checkLength, size,spanID,controlName)
{
document.getElementById(spanID).innerHTML='';
textElement=document.getElementById(textElement);
var num = textElement.value;
var format = /^[0-9]+$/;
if(checkRequired == true)
{
if(num == "" num == '' num == null)
{
document.getElementById(spanID).innerHTML= controlName+' is required';
return false;
}
else
{
if(num.match(format))
{
if(checkLength == true)
{
if(num.length > size)
{
document.getElementById(spanID).innerHTML= 'Maximum is '+ size +' digits';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML= 'Invalid Number';
return false;
}
}
}
else
{
if(num == "" num == '' num == null)
{
return true;
}
else
{
if(num.match(format))
{
if(checkLength == true)
{
if(num.length > size)
{
document.getElementById(spanID).innerHTML= 'Maximum is '+ size + ' digits';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML= 'Invalid Number';
return false;
}
}
}
}

Mandatory fields validation JS

//Mandatory fields
function ValidateRequired(txtItem,spanID)
{
document.getElementById(spanID).innerHTML='';
txtItem=document.getElementById(txtItem);
var txt = txtItem.value;
if(txt == '' txt == null txt == "")
{
document.getElementById(spanID).innerHTML= "Value required";
return false;
}
else
{
return true;
}
}

Common validation for Email in JS

//E-Mail validation
/* mail is the id of text box. The argument required means the field is necessary or not. If we send the value
of true to required, It means this is a mendatory field otherwise this field is optional */
function ValidateEmail(mail,required,spanID)
{
document.getElementById(spanID).innerHTML='';
mail=document.getElementById(mail);
var email = mail.value;
var emailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}(comnetorgeduintmilgovarpabizaeronamecoopinfopromuseum))$/;
if(required == true)
{
if(email == '' email == null email == "")
{
document.getElementById(spanID).innerHTML="Email is required";
return false;
}
else
{
if (!email.match(emailFormat))
{
document.getElementById(spanID).innerHTML="Invalid email address";
return false;
}
else
{
return true;
}
}
}
else
{
if(email == '' email == null email == "")
{
return true;
}
else
{
if (!email.match(emailFormat))
{
document.getElementById(spanID).innerHTML="Invalid Mail ID";
return false;
}
else
{
return true;
}
}
}
}

inside ajax :- Textbox on Key up hit Servercode

js:-

function SetKeyUp(e)
{
document.onkeyup = AssignKeyPress.interceptKeypress;
}


AssignKeyPress = {
interval : 200,
lastKeypress : null,
interceptKeypress : function(e) {
var key;
if (window.event) {
key = window.event.keyCode;
} else if (e) {
key = e.which;
}
var text = String.fromCharCode(key);
var format = /^([A-Za-z0-9\s])+$/;
//alert(AssignKeyPress.interval);
if(text.match(format))
{
AssignKeyPress.lastKeypress = new Date().getTime();
var that = AssignKeyPress;
setTimeout(function() {
var currentTime = new Date().getTime();
if(currentTime - that.lastKeypress > that.interval) {
that.sendRequest();
}
}, that.interval + 100);
}
else if(key == 8 key == 46)
{
AssignKeyPress.lastKeypress = new Date().getTime();
var that = AssignKeyPress;
setTimeout(function() {
var currentTime = new Date().getTime();
if(currentTime - that.lastKeypress > that.interval) {
that.sendRequest();
}
}, that.interval + 100);
}
},
sendRequest : function() {
__doPostBack('ctl00$SiteContentPlaceHolder$searchTextBox','SearchTextBoxKeyPress');
}

C#:-


if (this.IsPostBack)
{
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if (eventArgument.Trim() == "SearchTextBoxKeyPress")
{
_setFocus = true;
SearchTextBoxKeyPress(selectTabHiddenField.Value);
}
else
{
_setFocus = false;
}
}

if (httpBrowserCapabilities.Browser == "Firefox")
{
searchTextBox.Attributes.Add("OnKeyUp", "SetKeyUp(this);");
}
else
{
searchTextBox.Attributes.Add("OnKeyUp", "AssignKeyPress.interceptKeypress(this);");
}

set focus issue in ajax

C#:-
(this.Master.FindControl("ScriptManager1") as System.Web.UI.ScriptManager).SetFocus(searchTextBox);

set cursor in end of the word in textbox firefox issue

js:-

function SetEnd(textBox)
{
if (textBox.createTextRange)
{
var fieldRange = textBox.createTextRange();
fieldRange.moveStart('character', textBox.value.length);
fieldRange.collapse();
fieldRange.select();
}
}
C#:-

HttpBrowserCapabilities httpBrowserCapabilities = new HttpBrowserCapabilities();

httpBrowserCapabilities = Request.Browser;

if (httpBrowserCapabilities.Browser != "Firefox")
{
searchTextBox.Attributes.Add("OnFocus", "SetEnd(this)");
}

email validation in javascript

function ValidateEmail(mail,spanID)
{
var email = mail;
var emailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}(comnetorgeduintmilgovarpabizaeronamecoopinfopromuseum))$/;
if (!email.match(emailFormat))
{
document.getElementById(spanID).innerHTML = "Invalid Mail ID -"+mail;
}
}

hit the serverside event from javascript

JS:-
__doPostBack('ctl00$SiteContentPlaceHolder$searchTextBox','SearchTextBoxKeyPress');

C#:-
inside pageload

if (this.IsPostBack)
{
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if (eventArgument.Trim() == "SearchTextBoxKeyPress")
{
_setFocus = true;
SearchTextBoxKeyPress(selectTabHiddenField.Value);
}
else
{
_setFocus = false;
}
}