Sunday, August 14, 2011

Browser name and version in javascript

<script type="text/javascript">
alert(navigator);
var browserName=navigator.appName;  
var userAgent = navigator.userAgent
alert(browserName);
    if (browserName=="Microsoft Internet Explorer"){
        alert("Microsoft Internet Explorer");
        alert("Microsoft Internet Explorer Version : "+navigator.appVersion);
        alert(document.documentMode;);
     
    }
    else if (browserName=="Netscape" && userAgent.indexOf("Chrome") != -1 ){
        alert("Google Chrome");
        alert("Google Chrome Version: "+navigator.appVersion);
    }
    else if(browserName=="Netscape" && userAgent.indexOf("Mozilla") != -1){
        alert("Mozilla Firefox");
        alert("Mozilla Firefox Version : "+navigator.appVersion);
    }else{
        alert("Another browser");
    }
</script> 

Javascript basic functionalities

document.getElementById("divId").style.margin='20px 20px 20px 20px';
document.getElementById("divId").style.marginTop='-2px';
document.getElementById("divId").style.marginBottom='-2px';
document.getElementById("divId").style.marginLeft='-2px';
document.getElementById("divId").style.marginRight='-2px';

document.getElementById("divId").style.padding='20px 20px 20px 20px';
document.getElementById("divId").style.paddingBottom='1px';
document.getElementById("divId").style.paddingTop='1px';
document.getElementById("divId").style.paddingLeft='1px';
document.getElementById("divId").style.paddingRight='1px';


document.getElementById("divId").style.height='146px';
document.getElementById("divId").style.width='146px';


document.getElementById("divId").style.position='relative';

document.getElementById("divId").style.display = 'NONE';
document.getElementById("divId").style.display = 'BLOCK';

document.getElementById("divId").style.visibility="visible";

document.getElementById("divId").style.backgroundColor="#CDDBF0";

Sunday, July 24, 2011

Validate Empty spaces in Javascript

function checkSpaces(elm , fieldName){
var name = elm.value
name = name.replace(/(\s+)+/g," ");
if(name == "" || name == " "){
alert(fieldName +" should not allow empty spaces");
elm.focus();
return true;
}
return false;
}

Check alpha numeric value in javascript.

function isAlphaNumeral(elm,field_name){
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789., ";
    if(!checkChars(elm.value,chars)){
            alert(field_name+" is invalid");
            elm.select();
            return false;
    }
    return true;
}



function checkChars(val,chars){  
    var flag = false;
    for(i=0; i < val.length; i++){
        ch_data = val.charAt(i);
        for(j=0; j
< chars.length; j++){          
            if(ch_data==chars.charAt(j)){
                flag = false;              
                break;
            }
            flag=true;
        }
        if(flag){          
            return false;
        }
    }
    return true;
}< val.length; i++){ < chars.length;    =""

Convert to Upper or Lower case in Javascript

function convertToUpper(field){
    field.value = field.value.toUpperCase();
    field.value = field.value.toLowerCase();
}