Saturday, January 15, 2011

Java Script Functions

1. Functions to count the text entered.

Note: Where "bl1" is the asp:lable

<script type="text/javascript">
function count(text)
{
if(text != null)
{
document.getElementById('bl1').innerHTML  = text.length;
}
}
</Script>

2. Validation for US Phone number : Format: (111)-111-1111

<script type="text/javascript">
function numvalidate(text)
{
var test_str = /^([\(]{1}[0-9]{3}[\)]{1}[\-]{1}[0-9]{3}[\-]{1}[0-9]{4})$/
if(test_str.test(text))
{
return true;
}
else
{
document.getElementById('bl1').innerHTML  = "please enter proper value";
return false;
}
}
</script>

3. Text input should be in "alphabets and space"

<script type="text/javascript">
function functions(text)
{
var test_str = /^[a-zA-Z ]+$/;
if(test_str.test(text))
{
return true;
}
else
{
alert("Invalid name");
return false;
}
}
</script>

4. Function to validate SSN:

<script type="text/javascript">
 function checkSsn(ssn)
 {
 var RE_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;
 if (RE_SSN.test(ssn))
 {
  alert("VALID SSN");
 }
 else
 {
  alert("INVALID SSN");
 }
</script>

5. Function to validate userid that is either email Id or user name.

function validatemail(text)
{
var test_str = /^\w+[\w-\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$/
if(text != null)
{
if (text.indexOf("@") == -1)
{
//do nothing
}
else
{
if(test_str.test(text))
{
return true;
}
else
{
window.alert("invalid EmailId");
return false;
}
}
}
else
{
window.alert("enter user name or email id");
return false;
}
}

No comments:

Post a Comment