Tuesday, January 18, 2011

Java Script Code to allow only one checkbox selection in GridView

To allow only one checkbox selection in GridView (like radio button selection from group).
Call the following function from checkbox onclick event like OnClick="CheckGridList();"
put the following code in javascript tag
function CheckGridList()
       {     
        var count=0;
        for (i=0; i < document.forms[0].elements.length; i++)
            {          
                if ((document.forms[0].elements[i].type == 'checkbox') &&
                    (document.forms[0].elements[i].name.indexOf('checkBox1') > -1))
                {
                    if (document.forms[0].elements[i].checked == true)
                    {                                  
                    count++;
                    if (count>1)
                        {
                            document.forms[0].elements[i].checked = false;                      
                            break;
                        }
                    }
                }
            }
            if (count > 1)
            {          
            alert('Please select only one checkbox from grid);
            return false;
            }
            else { return true;}
        }

Monday, January 17, 2011

Uploadin a file and save it in a specified location

In this article, I am going to show how we can upload a file. Here I am uploading  a .txt file and saving it in a predefined location. Let see this by a simple programme. Here I am using  an input tag highlighted with yellow colour for browsing the uploaded file.

The fileupload.aspx code is:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 <title>File Upload Demostration</title>
</head>
 <body>
    <form id="Form1" method="post" runat="server">
     <table cellpadding="0" cellspacing="0" width="80%" align="center" border="4">
      <tr><td height="20px"></td></tr>
       <tr><td height="200px" align="center" valign="middle">
               <input id="MyFile"   type="file" size="81" name="File1" runat="server" />
                      <br /><br />
                             <asp:Button id="btnSubmit"   runat="server" Text="Submit" Width="139px"Height="30px" OnClick="btnSubmit_Click"></asp:Button>
                             <asp:Label id="lbl"   runat="server" Width="402px" Height="33px"></asp:Label>
                            
                             </td></tr></table>
                   </form>
</body>
</html>

The fileupload.aspx.cs is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (MyFile.PostedFile.ContentLength == 0)
        {
            lbl.Text = "Cannot upload zero length file";
            return;
        }
        lbl.Text = MyFile.PostedFile.FileName;
       MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");

    }
}

When user run the application then the window will look like this:

Figure 1.

When user click on Browse button:



Figure 2.

After selecting the file the window will look like this:



Figure 3.

When user click on submit button.



Figure 4. The uploaded file will save in the directory specified as the destination path in .cs file.

JavaScript Popup Boxes

JavaScript Popup Boxes




JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box

An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.

Syntax

alert("sometext");


Example

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>





Confirm Box

A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

Syntax

confirm("sometext");


Example

<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show confirm box" />

</body>
</html>





Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax

prompt("sometext","defaultvalue");


Example

<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
  {
  document.write("Hello " + name + "! How are you today?");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_prompt()" value="Show prompt box" />

</body>
</html>

Java script validations

Javascript Form Validation
_                  ____            _       _
     

Put all of the code examples  between these script tags.

<script language="JavaScript">
<!--

//-->
</script>



E-Mail format checking script.

if (document.formname.fieldname.value.length >0) {
  i=document.formname.fieldname.value.indexOf("@")
  j=document.formname.fieldname.value.indexOf(".",i)
  k=document.formname.fieldname.value.indexOf(",")
  kk=document.formname.fieldname.value.indexOf(" ")
  jj=document.formname.fieldname.value.lastIndexOf(".")+1
  len=document.formname.fieldname.value.length

  if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
  }
  else {
   alert("Please enter an exact email address.\n" +
  document.formname.fieldname.value + " is invalid.");
  return false;
  }

 }
 
Check if field has special characters.

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

  for (var i = 0; i < document.formname.fieldname.value.length; i++) {
   if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
   alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
   return false;
   }
  }
 
Check if certian radio buttons have not been selected.

if (!document.formname.fieldname[0].checked &&
     !document.formname.fieldname[1].checked &&
     !document.formname.fieldname[2].checked &&
     !document.formname.fieldname[3].checked) {
     alert("Please choose a group designation.\n");
     return false;
    }
  
Check if textbox has any characters in it.

if (document.formname.fieldname.value.length == 0) {
          alert("Please fill out your name.\n");
   return false;
     }
     
Check if multiple checkboxes have not been selected.Replace false with true to see if all are selected.

if (document.formname.fieldname.checked == false &&
         document.formname.fieldname.checked == false &&
  document.formname.fieldname.checked == false) {
   
  alert("Please select at least one checkbox.\n");
  return false;
     }
  
Check drop down has been selected. Set drop down's value to Not_Selected for this to work.

if (document.formname.fieldname.value == 'Not_Selected') {
          alert("Please provide us with a selection.\n");
          return false;
     }
 
Scan values in a field and if they are all letters then alert. The second block of code does the same but for numbers

//alert on finding all letters
    var noalpha = /^[a-zA-Z]*$/;
if (noalpha.test(document.formname.fieldname.value)) {
     alert("Please enter at least one number in the \"username\" field.");
     return false;
}

//alert on finding all numbers
var nonums = /^[0-9]*$/;
if (nonums.test(document.formname.fieldname.value)) {
     alert("Please enter at least one letter in the \"username\" field.");
     return false;
}
 
Remove special characters from a string.

function clearText() {
     document.formname.fieldname.value=filterNum(document.formname.fieldname.value)

     function filterNum(str) {
          re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
          // remove special characters like "$" and "," etc...
          return str.replace(re, "");
     }
}
  
Detect special characters in text box. Or any character you subsitute for the special characters.

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        for (var i = 0; i < document.formname.fieldname.value.length; i++) {
                if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
                alert ("The box has special characters. \nThese are not allowed.\n");
                return false;
        }
                }