Wednesday, January 4, 2012

What is difference b/w Javascript ,Jscript and Jquery????

JavaScript is the common term for a combination of the ECMAScript programming language plus some means for accessing a web browser's windows and the document object model (DOM).

JScript is Microsoft's version of JavaScript. The programming language itself is very similar to ECMAScript, but the DOM access differs in many respects.

JQuery is a JavaScript library that handles many commonly used features and also handles the differences between, e.g., Internet Explorer and standards-compliant browsers. It's something you can use to reduce the amount of work when creating web-based applications.

Tuesday, January 3, 2012





Functions 
----------
1) can be used with Select statement
2) Not returning output parameter but returns Table variables
3) You can join UDF
4) Cannot be used to change server configuration
5) Cannot be used with XML FOR clause
6) Cannot have transaction within function

Stored Procedure
-----------------
1) have to use EXEC or EXECUTE
2) return output parameter
3) can create table but won’t return Table Variables
4) you can not join SP
5) can be used to change server configuration
6) can be used with XML FOR Clause
7) can have transaction within SP


--------------------

1) functions are used for computations where as procedures 
can be used for performing business logic
2) functions MUST return a value, procedures need not be.
3) you can have DML(insert, update, delete) statements in a 
function. But, you cannot call such a function in a SQL 
query..eg: suppose, if u have a function that is updating a 
table.. you can't call that function in any sql query.- 
select myFunction(field) from sometable; will throw error.
4) function parameters are always IN, no OUT is possible
 ------------------------------------------------------------------------------------------------------

a. A FUNCTION is always returns a value using the return 
statement. A  PROCEDURE may return one or more values 
through parameters or may not return at all.
b. Functions are normally used for computations where as 
procedures are normally used for executing business logic.
c. A Function returns 1 value only. Procedure can return 
multiple values (max 1024).
d. Stored procedure returns always integer value by default 
zero. Whereas function returns type could be scalar or 
table or table values
e. Stored procedure is precompiled execution plan where as 
functions are not.
f. A function can call directly by SQL statement like 
select func_name from dual while procedure cannot.
g.Stored procedure has the security and reduces the network 
traffic and also we can call stored procedure in any no. of 
applications at a time.
h. A Function can be used in the SQL Queries while a 
procedure cannot be used in SQL queries .that cause a major 
difference b/w function and procedures.

==================================================================================
Although both functions and sp's are prcomiled sql statements there exists some differences between them.
-------------------------------------------------------------------------------------------------------

1. Functions must return a value(scalar,inline table or multi statement table) whereas stored proc may or may not retun a value.
2.Functions can return a table whereas stored procs can create a table but can't return table.
3. Stored procs can be called independently using exec keyword whereas function are called using select statements.
4. Stored procs can be used to change server configuration(in terms of security-i.e. setting granular permissions of user rights) whereas function can't be used for this
5. XML and output parameters can't be passed to functions whereas it can be with sp's.
6.transaction related statement can be handled in sp whereas it can't be in function.
7. stored procedures can call a funtion or another sstored proc similarly a function can call another function and a stored proc.The catch with function is that no user defined stored proc can be called.Only extended/system defined procs can be called.

Monday, January 2, 2012

Limit no of character in textbox with count in Asp.net using javascript


html page:
=======
<script type="text/javascript">
function textCounter(field,maxlimit)
{
if (field.value.length > maxlimit)
{
return false;
}
}
</script>
Page Load Events:
=============
protected void Page_Load(object sender, EventArgs e)
{
txtmaxlen.Attributes.Add("onkeydown", "return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onkeyup", "return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onmousedown","return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onmouseup","return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onblur", "return textCounter(this, 5)");
}



How to Validate File Extension for ASP.NET FileUpload Control



In this article I am explaining How to Create a File Extension Filter for the ASP.Net FileUpload Control

On many occasions there’s a requirement to upload only selected types of files and reject the others

In this article I will explain both Client Side and Server Side validation of files using their extensions

Client Side Validation:

<script type="text/javascript">
    var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "xls"];
    function ValidateFile() {
        var file = document.getElementById("<%=FileUpload1.ClientID%>");
        var label = document.getElementById("<%=Label1.ClientID%>");
        var path = file.value;
        var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
        var isValidFile = false;
        for (var i = 0; i < validFilesTypes.length; i++) {
            if (ext == validFilesTypes[i]) {
                isValidFile = true;
                break;
            }
        }
        if (!isValidFile) {
            label.style.color = "red";
            label.innerHTML = "Invalid File. Please upload a File with" +
    " extension:\n\n" + validFilesTypes.join(", ");
        }
        return isValidFile;
    }
</script>





As you can see above I have an array validFileTypes in which I am storing the extensions of the files that I want to allow the user to upload based. Then it loops through the array and matches that with that of the file selected by the user if it does not match user is prompted to select a valid file.

You can add the extensions of the File types that you want to allow to the array as shown in the animated GIF below.


Server Side Validation:

C#


protected void btnUpload_Click(object senderEventArgs e)
{
    string[] validFileTypes = { "bmp""gif""png""jpg""jpeg""doc""xls" };
    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    bool isValidFile = false;
    for (int i = 0; i < validFileTypes.Length; i++)
    {
        if (ext == "." + validFileTypes[i])
        {
            isValidFile = true;
            break;
        }
    }
    if (!isValidFile)
    {
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "Invalid File. Please upload a File with extension " +
                       string.Join(",", validFileTypes);
    }
    else
    {
        Label1.ForeColor = System.Drawing.Color.Green;
        Label1.Text = "File uploaded successfully.";
    }
}



VB.NET 

    Protected Sub btnUpload_Click(ByVal sender As ObjectByVal e As System.EventArgs)
        Dim validFileTypes As String() = {"bmp""gif""png""jpg""jpeg""doc""xls"}
        Dim ext As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)
        Dim isValidFile As Boolean = False
        For i As Integer = 0 To validFileTypes.Length - 1
            If ext = "." & validFileTypes(i) Then
                isValidFile = True
                Exit For
            End If
        Next
        If Not isValidFile Then
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = "Invalid File. Please upload a File with extension " & _
                            String.Join(",", validFileTypes)
        Else
            Label1.ForeColor = System.Drawing.Color.Green
            Label1.Text = "File uploaded successfully."
        End If
    End Sub


As you will notice the Server Side File Extension validation also use the same logic as used in client side validation checking. Here also I am maintaining a string array of valid File extensions and thenmatching it with the extension of the File that has been uploaded. The Server Side Validation Checking ensures that the even if the JavaScript Client Side Checking fails it can be validated server side.

 I am calling both Server Side and Client Side Validation methods on the Click event of the Upload button as shown below. I have also used a label which will display the error or success messages.


<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
<asp:Button ID="btnUpload" OnClick="Upload" runat="server" Text="Upload"OnClick="btnUpload_Click" OnClientClick="return ValidateFile()"></asp:Button>

how to validate file type in file upload control using JavaScript and diffrent type of regular expressions to validate file upload control in asp.net



Tuesday, December 14, 2010 
Introduction:

Here I will explain how to validate file type in file upload control using JavaScript.

Description:

I have one page that contains one file upload control to accept files from user and saving it in one folder. Here I need to give permission for user to upload only .doc or .docx files for that I have written validation by using JavaScript to validate files before save it in database.

Here I have some of regular expressions to validate file upload. 



Regular expression to validate file formats for .mp3 or .MP3 or .mpeg or .MPEG or m3u or M3U

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/;

Regular expression to validate file formats for .doc or .docx

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;

Regular expression to validate file formats for .txt or .TXT

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.TXT)$/;

Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;


Here my requirement is to validate file for only .doc or .docx for that I have written following code in aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function validate() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;
if (uploadcontrol.length > 0)
{
//Checks with the control value.
if (reg.test(uploadcontrol))
{
return true;
}
else
{
//If the condition not satisfied shows error message.
alert("Only .doc, docx files are allowed!");
return false;
}
}
//End of function validate.
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<b>Restrict File Upload sample</b> <br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
<br />
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 

 Demo

 
Other Related validation posts