Tuesday, August 9, 2011

JavaScript Validation for TextBox (mm/dd/yyyy)

Call this function on Button OnClientClick
function ValidateDate()
    {
        var txtSDate = document.getElementById('<%=txtSDate.ClientID%>');
        var txtEndDate = document.getElementById('<%=txtEndDate.ClientID%>');
       
        if(txtSDate.value != '')
        {
            if (isDate(txtSDate.value)==false){
            txtSDate.focus();           
            return false;
            }
        }
       
        if(txtEndDate.value != '')
        {
            if (isDate(txtEndDate.value)==false){
            txtEndDate.focus();           
            return false;
            }
        }     
     }

Now Attach this functions in a JS file and attach to aspx:
--------------------------------Under JS File----------------------------------------------------------
function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){  
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){  
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
        if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){

    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;

    var daysInMonth = DaysArray(12)
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strMonth=dtStr.substring(0,pos1)
    var strDay=dtStr.substring(pos1+1,pos2)
    var strYear=dtStr.substring(pos2+1)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if (pos1==-1 || pos2==-1){
        alert("The date format should be : mm/dd/yyyy")
        return false
    }
    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month")
        return false
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        alert("Please enter a valid date")
        return false
    }
return true
}
-----------------------------------------------------------------------------------------------------------




Monday, July 18, 2011

Making Image Background to transparent

Use Photofiltre for making image background to tranparent.
  1. load the image
  2. go to Image > Transparent Color...
  3. choose the color (white), set the desired tolerance with the slider, click OK
  4. go to File > Save as... and save the image as .PNG

Wednesday, June 1, 2011

Wrapping Text Line in a Label

Add this Style to Label control:
Style="font-family: Verdana; word-wrap: break-word; white-space: pre-wrap;
                            white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap;"


Thursday, January 6, 2011

A simple JavaScript Email Validation

This function will validate the e-mail address
function ValidateEmail(emailid)
   {
        
        var RegExEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
        if (!RegExEmail.test(emailid.value))
        {
            //alert("The E-mail address entered is not valid!");
            return true;
        }
        else
        {            
            return false;
        }
       
   } 

Thursday, February 11, 2010

Using Custom Validator for validating GridView

JS function---

 function ValidateGrid(source, args) {
            var cnt = 0;
            //Get target base & child control.
            var TargetBaseControl = document.getElementById('ctl00_ContentPlaceHolder1_GdUsers');
            var TargetChildControl = "ChkSel";

            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName("input");

            //Checked/Unchecked all the checkBoxes inside the GridView.
            for (var n = 0; n < Inputs.length; ++n) {
                if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl, 0) >= 0) {
                    if (Inputs[n].checked == true)
                        cnt += 1;
                }
            }
            if (cnt > 0) {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
        }

Button which causes validation--

 <asp:ImageButton ID="cmdDelete" runat="server" ValidationGroup="Val1"
                                runat="server" Text="Delete" OnClick="BtnDelete_Click" CssClass="a"
                                Height="37px" ImageUrl="~/Images/Delete.gif" Width="118px" />

CustomValidator--

<asp:CustomValidator ID="CusVal1" runat="server" Display="Dynamic" ClientValidationFunction="ValidateGrid" ControlToValidate="cmdDelete"
                    ErrorMessage="Select any user/users!"></asp:CustomValidator>

a Sql Query to check and concatenate multiple col values into one column

update YourTable
set ColName=case when Col1 IS NOT NULL then (case when Col2 IS NOT NULL then Col1+'|'+Col2 else Col1 end) else NULL end

Friday, January 22, 2010

Something about String and StringBuilder

1.string is immutable where is string builder is mutable
2.In stringBuilder, automatically increase and decrease size of string object whenever add elements and delete elements .Where in string , it is fixed size .
3.StringBuilder can perform a insert(),Delete(),Reverse(),Sort() operations etc and we can create multiple instances .Where as String , It  can't perform.
4.StringBuilder takes enough memory for variable .Where as String can't perform.

we use strings for non-changing strings because every time when a string is changed a new instance of string is created using new memory locations. whereas in stringbuilder we can work on the same memory location.
so stringbuilder is efficient in terms of memory for changing strings.