Powered By Blogger

Thursday, December 17, 2009

Regular Expression in asp.net


This table lists the regular expressions which were used in this sample code, the data type each is intended to recognize, and some examples of strings that each regular expression accepts (matches) and rejects (does not match).

data type regular expression accepts rejects
whitespace /^\s+$/ spaces, tabs, etc. 0 a
single letter /^[a-zA-Z]$/ a A 0 . -
alphabetic string /^[a-zA-Z]+$/ aAbB a1 23
single digit /^\d/ 0 00 1a
single letter or digit /^([a-zA-Z]|\d)$/ 0 a 00 1a
alphanumeric string /^[a-zA-Z0-9]+$/ a1b2 2.5 1.a
integer /^\d+$/ 0 23 23.5
signed integer /^(+|-)?\d+$/ -1 +12 -1.1
floating point number /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/ 1.1 .9 8. -1.1 .
signed float /^(((+|-)?\d+(\.\d*)?)|((+|-)?(\d*\.)?\d+))$/ -1.1 1.1 .9 8. 1a
email address /^.+\@.+\..+$/ a@b.c a@b




JavaScript

Javascript function to check textbox empty.

function UnamePwdValidation()
{
if (document.getElementById("<%=Txt_UserName.ClientID%>").value=="")
{
alert("Enter Username");
document.getElementById("<%=Txt_UserName.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=Txt_Password.ClientID %>").value=="")
{
alert("Enter Password");
document.getElementById("<%=Txt_UserName.ClientID %>").focus();
return false;
}
}

< asp:ImageButton ID="Submit" OnClientClick="return UnamePwdValidation()" runat="server" ImageUrl="images\Go.jpg" TabIndex="3" /
>


Javascript function to allow only alphabets (lower and upper case).

function AllowCharacterOnly(myevent)
{
var Val = (myevent.which) ? myevent.which : myevent.keyCode
if ((Val > 64 && Val <> 96 && Val < type="text" id="txtChracter" onkeypress="return AllowCharacterOnly(event)" style="color: rgb(255, 102, 0); font-weight: bold;">Javascript to allow user to enter only numeric value.


function AllowNumericValue(myevent)
{
var charVal = (myevent.which) ? myevent.which : myevent.keyCode
if (charVal > 31 && (charVal <> 57))
{
return false;
}
return true;

}


input type="text" id="Text2" onkeypress="return AllowNumericCharacters(event)"

Javascript function to show an alert if user entered a non numeric value.

function ValidationCheckforNumeric(txtVal)
{
var len = txtVal.value.length;
var charVal = 0;
for (var i=0;i
{
charVal = txtVal.value.charCodeAt(i);
if (!((charVal >= 48 && charVal <= 57))) { alert("Only Numeric Values are Allowed"); txtVal.value = ''; txtVal.focus(); return false; } } return true; } input type="text" id="txtNumber" onchange='ValidationCheckforNumeric(this)' Javascript code for checking null value in Textbox.

function CheckForEmptyTextbox()
{
var s;
var txtValue;
txtValue = document.getElementById('TextBox1');
if (txtValue.value == '')
{
s = confirm('TextBox1 is blank, do you want to continue?');
return s;
}
return true;

}

Button1.Attributes.Add("onclick", "javascript: CheckForEmptyTextbox();");

On button click - how to bring up browsers emailing box using javascript.

INPUT TYPE="button" VALUE="Send Email." onClick = "parent.location= 'mailto:mymailid@gmail.com'"

Show Timer on status bar using Javascript

function showtime ()
{

var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes <>= 12) ? " P.M." : " A.M."

window.status = timeValue;

}

Call this function on load of the form.

Check the browser type using javascript.

if(navigator.appName=="Netscape")
{
alert("Browser is Netscape Navigator.");
}
else if (navigator.appName == "Microsoft Internet Explorer")
{
alert("Browser is Microsoft Internet Explorer.");
}
else
{
alert("Neither Netscape Navigator nor Internet Explorer.");
}

Set Control Focus using Javascript code.

body onload="document.forms[0]['TextBox1'].focus();"

PopUp Window

window.open('pop_up.html', 'popup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=200,height=130')

Cookies (JavaScript)

http://www.pages.org/javascript/index.html