Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Wednesday 27 June 2012

Printing the content of div or panel

In general, we required to print the content of a portion of page. We don't need that whole page will get printed. So here I am going to provide a small java script function that can solve this problem.
Here is the code for print a specific area.

function PrintDiv() {    
var prtContent = document.getElementById('<%=divPrintArea.ClientID%>');   
var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,
toolbar=1,scrollbars=1,status=0');    
WinPrint.document.write(prtContent.innerHTML);    
WinPrint.document.close();   
WinPrint.focus();   
WinPrint.print();   
WinPrint.close();    
}

Javascript function to check integer value

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;
}

Making a button default clickable when enter key is pressed

Many times we want that on pressing enter key the submit button should be automatic click. For example on login page after typing password generally users press enter key and want that submit button will call but nothing happen.
 Here is the JavaScript code for it:


function doClick(buttonName, e) {
            var key;
            if (window.event)
                        key = window.event.keyCode;     //IE
            else
                        key = e.which;     //firefox
            if (key == 13) {   //Get the button the user wants to have clicked
                        var btn = document.getElementById(buttonName);
                        if (btn != null) { //If we find the button click it
                                    btn.click();
                                    event.keyCode = 0
                        }
            }
}

Tuesday 26 June 2012

How to check if a string contains special character using javascript


Sometimes we need to check if a string contains any special charachter. Ex. name should not contains any special character.

Here is the code for it : 

function IsSpecialChar(strString)
//  check for valid SpecialChar strings
{
    var strValidChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}




Happy Learning......