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;
}
}

Thursday, January 6, 2011

Including Captcha in Asp.net Application

Step1: Download MSCaptcha.dll from the Internet. Include it in your bin folder.


Step2: In Web.config file paste these line.


<httpHandlers>
<add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
</httpHandlers>


Step3:  Place this in aspx page where you want to include Captcha.


<cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="40" CaptchaWidth="150" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" />

Step4:  Put a textbox somewhere in your page where your user must enter what he sees in your captcha.

 <asp:TextBox ID="txtCap1" runat="server" ></asp:TextBox>

Step5:  Put this in your .cs file to validate user input.


ccJoin.ValidateCaptcha(txtCap1.Text);
if (!ccJoin.UserValidated)
{
//Inform user that his input was wrong ...

return;

}

divyashree.k

Tuesday, January 4, 2011

Send a mail Using System.Net.Mail

Using System.Net.Mail; // include system.net.mail name space in your application

public class SendMail
{
   public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
   {
      MailMessage mMailMessage = new MailMessage();
      mMailMessage.From = new MailAddress(from);
      mMailMessage.To.Add(new MailAddress(to));
      if ((bcc != null) && (bcc != string.Empty))
      {
         mMailMessage.Bcc.Add(new MailAddress(bcc));
      }
      if ((cc != null) && (cc != string.Empty))
      {
         mMailMessage.CC.Add(new MailAddress(cc));
      }  
      mMailMessage.Subject = subject;
      mMailMessage.Body = body;
      mMailMessage.IsBodyHtml = true;
      mMailMessage.Priority = MailPriority.Normal;
      SmtpClient mSmtpClient = new SmtpClient();
      mSmtpClient.Send(mMailMessage);
   }
}
Call this function in Your Code Where required :
SendMail.SendMailMessage("fromAddress@yourdomain.com", "test@yahoo.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
Web.config :
<?xml version="1.0"?>
<configuration>
   <system.net>
      <mailSettings>
         <smtp from="defaultEmail@yourdomain.com">
            <network host="smtp.yourdomain.com" port="25" userName="yourUserName"  password="yourPassword"/>
         </smtp>
      </mailSettings>
   </system.net>
</configuration>

divyashree.k

Ajax Model Popup Extender

<%@ Register Assembly="AjaxControlToolkit" Namespace = "AjaxControlToolkit" TagPrefix="CC1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Button ID="btnSignUp" runat="server" Text="Sign Up" onclick="btnSignUp_Click" />
</div>

<CC1:ModalPopupExtender runat = "server" ID= "ModalPopupExtender1" BehaviorID="mpeNewContacts" TargetControlID = "dummy" CancelControlID = "btnClose" PopupControlID = "test" DropShadow="true"></CC1:ModalPopupExtender>
<a href="#" runat="server" id="dummy"> </a>
<div id = "test" runat = "server" style="padding: 15px; width: 370px; color: Black; font-family: Trebuchet MS; font-size: 13px; background-color: #F8F8F8; border: Solid 1px #666666; min-height: 270px; ">
<div align ="right" style ="width:100%; height: 30px; margin-top:5px;">
<asp:Button runat = "server" ID = "btnSend" Text = "Send" />
<input id ="btnClose" type="button" value="Close" runat="server" />
</div>

</div>
</asp:Content>

Code Bind:

protected void btnSignUp_Click(object sender, EventArgs e)
{
     ModalPopupExtender1.Show();
}

divyashree.k