Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘validation’

ASP.NET Validation Controls

Posted by Viral Sarvaiya on March 13, 2009


With ASP.NET, there are six(6) controls included.

They are:
* The RequiredFieldValidation Control
* The CompareValidator Control
* The RangeValidator Control
* The RegularExpressionValidator Control
* The CustomValidator Control

All of the validation controls inherit from the base class BaseValidator so they
all have a series of properties and methods that are common to all validation controls.
They are:

* ControlToValidate – This value is which control the validator is applied to.
* ErrorMessage – This is the error message that will be displayed in the validation summary.
* IsValid – Boolean value for whether or not the control is valid.
* Validate – Method to validate the input control and update the IsValid property.
* Display – This controls how the error message is shown. Here are the possible options:
o None (The validation message is never displayed.)
o Static (Space for the validation message is allocated in the page layout.)
o Dynamic (Space for the validation message is dynamically added to the page if validation fails.)

The RequiredFieldValidation Control
———————————–

The first control we have is the RequiredFieldValidation Control. As it’s obvious,
it make sure that a user inputs a value. Here is how it’s used:

Required field: <asp:textbox id=”textbox1″ runat=”server”/>
<asp:RequiredFieldValidator id=”valRequired” runat=”server” ControlToValidate=”textbox1″
ErrorMessage=”* You must enter a value into textbox1″ Display=”dynamic”>*
</asp:RequiredFieldValidator>

The CompareValidator Control
—————————–
Next we look at the CompareValidator Control. Usage of this CompareValidator is for
confirming new passwords, checking if a departure date is before the arrival date, etc.
We’ll start of with a sample:

Textbox 1: <asp:textbox id=”textbox1″ runat=”server”/><br />
Textbox 2: <asp:textbox id=”textbox2″ runat=”server”/><br />
<asp:CompareValidator id=”valCompare” runat=”server”
ControlToValidate=”textbox1″ ControlToCompare=”textbox2″
Operator=”Equals”
ErrorMessage=”* You must enter the same values into textbox 1 and textbox 2″
Display=”dynamic”>*
</asp:CompareValidator>

Here we have a sample where the two textboxes must be equal. The tags that are unique to this control is the ControlToCompare attribute which is the control that will be compared. The two controls are compared with the type of comparison specified in the Operator attribute. The Operator attribute can contain Equal, GreterThan, LessThanOrEqual, etc.
Another usage of the ComapareValidator is to have a control compare to a value. For example:

Field: <asp:textbox id=”textbox1″ runat=”server”/>
<asp:CompareValidator id=”valRequired” runat=”server” ControlToValidate=”textbox1″
ValueToCompare=”50″
Type=”Integer”
Operator=”GreaterThan”
ErrorMessage=”* You must enter the a number greater than 50″ Display=”dynamic”>*
</asp:CompareValidator>

The RangeValidator Control
————————–

Range validator control is another validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Sample:

Enter a date from 1998:
<asp:textbox id=”textbox1″ runat=”server”/>
<asp:RangeValidator id=”valRange” runat=”server”
ControlToValidate=”textbox1″
MaximumValue=”12/31/1998″
MinimumValue=”1/1/1998″
Type=”Date”
ErrorMessage=”* The date must be between 1/1/1998 and 12/13/1998″ Display=”static”>*</asp:RangeValidator>

The RegularExpressionValidator Control
————————————–

The regular expression validator is one of the more powerful features of ASP.NET.
Everyone loves regular expressions. Especially when you write those really big nasty ones…
and then a few days later, look at it and say to yourself. What does this do?
Again, the simple usage is:

E-mail: <asp:textbox id=”textbox1″ runat=”server”/>
<asp:RegularExpressionValidator id=”valRegEx” runat=”server”
ControlToValidate=”textbox1″
ValidationExpression=”.*@.*\..*”
ErrorMessage=”* Your entry is not a valid e-mail address.”
display=”dynamic”>*
</asp:RegularExpressionValidator>

The CustomValidator Control
—————————–

The final control we have included in ASP.NET is one that adds great flexibility to our
validation abilities. We have a custom validator where we get to write out own functions
and pass the control value to this function.

Field: <asp:textbox id=”textbox1″ runat=”server”>
<asp:CustomValidator id=”valCustom” runat=”server”
ControlToValidate=”textbox1″
ClientValidationFunction=”ClientValidate”
OnServerValidate=”ServerValidate”
ErrorMessage=”*This box is not valid” dispaly=”dynamic”>*
</asp:CustomValidator>

We notice that there are two new attributes ClientValidationFunction and OnServerValidate.
These are the tell the validation control which functions to pass the controltovalidate value to.
ClientValidationFunction is usually a javascript funtion included in the html to the user.
OnServerValidate is the function that is server-side to check for validation if client does
not support client-side validation.

Client Validation function:

<script language=”Javascript”>
<!–
/* … Code goes here … */
–>
</script>

>

Server Validation function:

Sub ServerValidate (objSource As Object, objArgs As ServerValidateEventsArgs)
‘ Code goes here
End Sub

Validation Summary
——————-

ASP.NET has provided an additional control that complements the validator controls.
This is the validation summary control which is used like:

<asp:ValidationSummary id=”valSummary” runat=”server”
HeaderText=”Errors:”
ShowSummary=”true” DisplayMode=”List” />

Advertisements

Posted in ASP.NET | Tagged: , , , , , , | Leave a Comment »

file upload validation with javascript

Posted by Viral Sarvaiya on March 13, 2009


step 1 : Make a default.aspx page

put this script in to head section of html page.

<script>

function valid_file()

{

if(!ChkFile(document.getElementById(“fileupload1”)))
{
alert(‘Please Upload Image File’);
document.getElementById(“fileupload1”).focus();
return false;
}

}

function ChkFile(txtfield)
{
var filename,filenamelen
var fileext,fileextlen
var allowedtypes = [“.jpg”,”.JPG”,”.jpeg”,”.JPEG”,”.BMP”,”.bmp”]
var dotpos
filename = txtfield.value
filenamelen = filename.length
dotpos = filename.lastIndexOf(“.”)
fileextlen = (filenamelen – dotpos – 1)

if (fileextlen >= 3 && fileextlen <= 4) //this allows files with a min of 3 and max of 4 chars in their extension
{
fileext = filename.substring(dotpos,filenamelen)
//alert (fileext)
for(var ctr=0;ctr<allowedtypes.length;ctr++)
{

if(allowedtypes[ctr] == fileext)
{
return true;
}
}
//alert(“Only JPG/JPEG/GIF type picture files are allowed.\n Please select a valid file type.”)

}
else
//alert(“The file you have selected is not a valid picture file.\nPlease select a different file.”)

return false
}

</script>

step 2 :On the  default.aspx.vb page

add this code in to page load function

dont put this file in ispostback block

btnsubmit..Attributes.Add(“onclick”, “javascript:return valid_file();”)

Posted in ASP.NET | Tagged: , , , , , | Leave a Comment »

 
%d bloggers like this: