Detecting the major browser names in javascript
Filed in Javascript Leave a comment
I found this very useful script on below website.
http://www.javascripter.net/faq/browsern.htm
Code from the above website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
var nVer = navigator.appVersion; var nAgt = navigator.userAgent; var browserName = navigator.appName; var fullVersion = ''+parseFloat(navigator.appVersion); var majorVersion = parseInt(navigator.appVersion,10); var nameOffset,verOffset,ix; // In Opera, the true version is after "Opera" or after "Version" if ((verOffset=nAgt.indexOf("Opera"))!=-1) { browserName = "Opera"; fullVersion = nAgt.substring(verOffset+6); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In MSIE, the true version is after "MSIE" in userAgent else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { browserName = "Microsoft Internet Explorer"; fullVersion = nAgt.substring(verOffset+5); } // In Chrome, the true version is after "Chrome" else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) { browserName = "Chrome"; fullVersion = nAgt.substring(verOffset+7); } // In Safari, the true version is after "Safari" or after "Version" else if ((verOffset=nAgt.indexOf("Safari"))!=-1) { browserName = "Safari"; fullVersion = nAgt.substring(verOffset+7); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In Firefox, the true version is after "Firefox" else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) { browserName = "Firefox"; fullVersion = nAgt.substring(verOffset+8); } // In most other browsers, "name/version" is at the end of userAgent else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) { browserName = nAgt.substring(nameOffset,verOffset); fullVersion = nAgt.substring(verOffset+1); if (browserName.toLowerCase()==browserName.toUpperCase()) { browserName = navigator.appName; } } // trim the fullVersion string at semicolon/space if present if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix); if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix); majorVersion = parseInt(''+fullVersion,10); if (isNaN(majorVersion)) { fullVersion = ''+parseFloat(navigator.appVersion); majorVersion = parseInt(navigator.appVersion,10); } document.write('Browser name = '+browserName+'<br>'); document.write('Full version = '+fullVersion+'<br>'); document.write('Major version = '+majorVersion+'<br>'); document.write('navigator.appName = '+navigator.appName+'<br>'); document.write('navigator.userAgent = '+navigator.userAgent+'<br>'); |
Disable copy paste in textbox asp.net
Filed in .Net | Javascript Leave a comment
<asp:TextBox ID=”txtEmailAddress2″ runat=”server” MaxLength=”50″ oncut=”return false;” onpaste=”return false;” oncopy=”return false;” onkeydown=”return disableCtrlKeyCombination(event);”></asp:TextBox>
<script type=”text/javascript”>
function disableCtrlKeyCombination(e) {
key = e.which; //firefox
if (e.ctrlKey) {
return false;
}
}
</script>
Getting values from label, hiddenfield in javascript for IE, Firefox
Filed in .Net | Javascript Leave a comment
var tot;
//Get cialis super active controls as below
var subTotals = document.getElementById(‘<%= lblSubTotals.ClientID %>’);
var promocodetype = document.getElementById(‘<%= hdnPromoCodeType.ClientID %>’);
if (document.all) {//IE
var pc = promocodetype.value;
tot = subTotals.innerText;
}
else{//Fire Fox
tot = subTotals.textContent;
}
Check at least one checkbox is selected in CheckBoxList using ASP.Net Validators
Filed in .Net | Javascript Leave a comment
I found a very good article to check that at least one chekbox is selected in a checkboxlist control. Works exactly like required field validator.
http://forums.asp.net/t/1000658.aspx (check answer from user yasserzaid)
<script type=”text/javascript”>
function ValidateRolesSelection(source, args)
{
//if your checkboxlist control is on simple asp.net page
var chkListModules = document.getElementById(‘<%= RolesCheckBoxList.ClientID %>’);
//if your checkboxlist control is inside some user control for e.g in CreateUserWizard
//var chkListModules = document.getElementById(‘<%= (CreateUserWizard1.WizardSteps[0].Controls[0].FindControl(“RolesCheckBoxList”) as CheckBoxList).ClientID %>’);
var chkListinputs = chkListModules.getElementsByTagName(“input”);
for (var i = 0; i < chkListinputs.length; i++)
{
if (chkListinputs[i].checked)
{
args.IsValid = true;
return;
}
}
args.IsValid = false;
}
</script>
Checking character/string length using .net validators
Filed in .Net | Javascript Leave a comment
I need to check character length between 9 to 24 need to be all numbers. I have applied below regular expression validator.
<asp:RegularExpressionValidator ID=”revBankAccountNo” runat=”server” ControlToValidate=”txtBankAccountNo” Display=”Dynamic” SetFocusOnError=”True” ValidationExpression=”^[0-9]{4,24}$”>*</asp:RegularExpressionValidator>
I need to check character length to be exact 9. I have applied below regular expression validator.
<asp:RegularExpressionValidator ID=”revBankAccountNo” runat=”server” ControlToValidate=”txtBankAccountNo” Display=”Dynamic” SetFocusOnError=”True” ValidationExpression=”^[0-9]{9}$”>*</asp:RegularExpressionValidator>