Modal Popup:
function openChangePassword() {
var $properties;
//setting properties of modalpopup.
$properties = "center=yes;status=no;scroll=yes;help=no;dialogHeight=100px;dialogWidth=540px;";
var $ChangePassword = showModalDialog("<ModalPopup Page>");
if ($ChangePassword) {
window.location = window.location;
}
else {
window.close();
}
}
Disabling Right Click:
var clickmessage="Right click disabled on images!"
function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}
else if (document.layers) {
if (e.which == 3) {
alert(clickmessage);
return false;
}
}
else if (document.getElementById){
if (e.which==3&&e.target.tagName=="IMG"){
alert(clickmessage)
return false
}
}
}
function associateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}
if (document.all)
document.onmousedown=disableclick
else if (document.getElementById)
document.onmouseup=disableclick
else if (document.layers)
associateimages()
</script>
CheckboxList Validations:
var elementRef = document.getElementById('chklLOB');
var checkBoxArray = elementRef.getElementsByTagName('input');
var i = 0;
for (i = 0; i < checkBoxArray.length; i++) {
var checkBoxRef = checkBoxArray[i];
var count = 0;
if (checkBoxRef.checked) {
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
document.getElementById('lblSummaryLOB').innerText += ', ';
document.getElementById('lblSummaryLOB').innerText += labelArray[0].innerHTML;
count++;
}
if (count == 0) {
alert('Select a LOB.');
return false;
}
else
return true;
}
Disabling Backspace:
function DisableBackSpace() {
var object = event.srcElement;
if (event.keyCode == 8 && (object.type != "text" && object.type != "textarea" && object.type != "password")) {
return false;
}
}
Password Strength:
<script language="javascript" type="text/javascript">
function passwordChanged() {
var strength = document.getElementById('<%=lblStrength.ClientID %>');
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
var pwd = document.getElementById("<%=txtNewPassword.ClientID %>");
if (pwd.value.length == 0) {
strength.innerHTML = 'Type Password';
} else if (false == enoughRegex.test(pwd.value)) {
strength.innerHTML = 'More Characters';
} else if (strongRegex.test(pwd.value)) {
strength.innerHTML = '<span style="color:green">Strong!</span>';
} else if (mediumRegex.test(pwd.value)) {
strength.innerHTML = '<span style="color:orange">Medium!</span>';
} else {
strength.innerHTML = '<span style="color:red">Weak!</span>';
}
}
</script>
---------------------------------
<table>
<tr>
<td>
Email id :
</td>
<td align="left">
<asp:TextBox ID="txtEmailid" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvtxtEmailId" runat="server" ControlToValidate="txtEmailid"
ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Enter your old Password :
</td>
<td align="left">
<asp:TextBox ID="txtOldPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvOldpassword" runat="server" ControlToValidate="txtOldPassword"
ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Enter your New Password :
</td>
<td align="left">
<asp:TextBox ID="txtNewPassword" runat="server" MaxLength="20" onkeyup="return passwordChanged();" TextMode="Password"></asp:TextBox>
<asp:Label ID="lblStrength" runat="server" ></asp:Label>
<asp:RequiredFieldValidator ID="rfvNewPassword" runat="server" ControlToValidate="txtNewPassword"
ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Confirm New Password :
</td>
<td align="left">
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvConfirmPassword" runat="server" ControlToValidate="txtConfirmPassword"
ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvConfirmPassword" runat="server" ControlToValidate="txtConfirmPassword"
ControlToCompare="txtNewPassword" ErrorMessage="*" SetFocusOnError="true"></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false"
OnClick="btnCancel_Click" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblNotification" runat="server" Visible="false" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:LinkButton ID="lbLogin" runat="server" Text="Back to Login" CausesValidation="false" PostBackUrl="~/Login.aspx"></asp:LinkButton>
</td>
</tr>
</table>
To check for integers
function isMyInteger(s) {
var i;
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}
To Check Email format
function chkEmail(str) {
var at = "@"
var dot = "."
var lat = str.indexOf(at)
var lstr = str.length
var ldot = str.indexOf(dot)
if (str.indexOf(at) == -1)
{ return false }
if (str.indexOf(at) == -1 || str.indexOf(at) == lstr)
{ return false }
if (str.indexOf(dot) == -1 || str.indexOf(dot) == lstr) {
return false
}
if (str.indexOf(at, (lat + 1)) != -1) {
return false
}
if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
return false
}
if (str.indexOf(dot, (lat + 2)) == -1) { //-1
return false
}
if (str.indexOf(" ") != -1)
{ return false }
return true;
}
OR :
function emailValidation() {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.getElementById('txtEmailId').value;
if (reg.test(address) == false) {
alert('Email address should be in the format of abc@abc.com');
return false;
}
return true;
}
Check for Alphabets :
function AlphabetsOnly() {
var key = window.event.keyCode;
if (key < 65 || key > 90 && key < 97 || key > 122)
window.event.returnValue = false;
}
Check for Numbers :
function NumericOnly() {
var key = window.event.keyCode;
if (key == 46)
window.event.returnValue = true;
else if (key < 48 || key > 57)
window.event.returnValue = false;
}