/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Wednesday, April 30, 2014

Regular expressions in asp.net

asp:FileUpload Control

<asp:FileUpload ID="FileUpload7" runat="server" size="12" CssClass="inputcd" />

Only JPG, GIF, PNG allowed 
ValidationExpression="(.*\.([jJ][pP][gG]|[gG][iI][fF]|[pP][nN][gG])$)"

Only JPG  allowed
ValidationExpression="^.*\\.((j|J)(p|P)(e|E)?(g|G)|(j|J)(p|P)(g|G))$"

Only .mp3 & .wma file format allowed
ValidationExpression="^.*\\.((m|M)(p|P)(3|3)|(w|W)(m|M)(a|A))$"

Only .mp4, .avi, .flv, .wmv, .mov  file format allowed
ValidationExpression="^.*\\.((m|M)(p|P)(4|4)|(a|A)|(v|V)(i|I)|(f|F)(l|L)(v|V)|(w|W)(m|M)(v|V)|(m|M)(o|O)(v|V))$"


<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="FileUpload7" ValidationGroup="AddCampaign" ErrorMessage="The file format should be Image (jpg,gif,png)" ValidationExpression="(.*\.([jJ][pP][gG]|[gG][iI][fF]|[pP][nN][gG])$)" ForeColor="Red"></asp:RegularExpressionValidator>


HttpPostedFile file = (HttpPostedFile)(filupload1.PostedFile);
private bool IsImage(HttpPostedFile file)
    {
        // This checks for image type... you could also do filename extension checks and other things
        // but this is just an example to get you on your way
        return ((file != null) && System.Text.RegularExpressions.Regex.IsMatch(file.ContentType, "image/\\S+") && (file.ContentLength > 0));
    }

    private bool IsVideo(HttpPostedFile file)
    {
        // This checks for Video type... you could also do filename extension checks and other things
        // but this is just an example to get you on your way
        return ((file != null) && System.Text.RegularExpressions.Regex.IsMatch(file.ContentType, "video/\\S+") && (file.ContentLength > 0));
    }

    private bool IsAudio(HttpPostedFile file)
    {
        // This checks for Video type... you could also do filename extension checks and other things
        // but this is just an example to get you on your way
        return ((file != null) && System.Text.RegularExpressions.Regex.IsMatch(file.ContentType, "audio/\\S+") && (file.ContentLength > 0));
    }


Only ZIP file allowed
ValidationExpression="(.*\.([Zz][Ii][pP])$)"


<asp:RegularExpressionValidator ID="regs1" runat="server" ControlToValidate="FileUploade1" Display="Dynamic" ErrorMessage="Only zip file allowed." SetFocusOnError="true"                                                    ValidationGroup="AddCampaign" ValidationExpression="(.*\.([Zz][Ii][pP])$)"> </asp:RegularExpressionValidator>


Only String Numeric Allowed (User Name)

<asp:RegularExpressionValidator ID="revUserName" runat="server" Display="None" ErrorMessage="Invalid user name." ControlToValidate="txtUserName" ValidationGroup="LoginUserValidationGroup" ValidationExpression="^[a-zA-Z0-9'._]{1,30}$" />


Only String Numeric Special Character Allowed PSW

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="None"
SetFocusOnError="true" ErrorMessage="Invalid password format. Password must be between 6 and 20 characters long." ControlToValidate="txtPassword" ValidationGroup="LoginUserValidationGroup" ValidationExpression="(?!^[a-zA-Z]*$)^([a-zA-Z0-9\@]{6,20})$" />

Compare Validator

<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Start date should be less than End date!!" Type="Date" ControlToValidate="txtEndDate" Operator="GreaterThanEqual" ValidationGroup="AddCampaign" ControlToCompare="txtStartDate" SetFocusOnError="true"> </asp:CompareValidator>


Only Valid Email ID Allowed
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

One more expression ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

<asp:RegularExpressionValidator ID="revParticipantEmailId" runat="server" ErrorMessage="Please enter valid email id." Display="none" ControlToValidate="txtParticipantEmailId" Font-Size="Small" ValidationGroup="valApnaSuzukiImage" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />


Only Valid Website Address Allowed

ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"

One more expression
ValidationExpression="^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$"

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtanotherURL" ErrorMessage="Invalid" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" ValidationGroup="adsense" CssClass="sfError"> </asp:RegularExpressionValidator>


Only String Small and Caps Letter Allowed ValidationExpression="^\s*[a-zA-Z,\s/.]+\s*$"

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please enter valid name" ControlToValidate="txtName" ValidationGroup="valpartcipate" ValidationExpression="^\s*[a-zA-Z,\s/.]+\s*$" Display="None" SetFocusOnError="true" />


Only 10 Digit Mobile No Allowed
ValidationExpression="\d{10}"

<asp:RegularExpressionValidator ID="revMobileNo" runat="server" ErrorMessage="Please enter valid contact no" ControlToValidate="txtmobileno" ValidationGroup="valpartcipate" ValidationExpression="\d{10}" Display="None" SetFocusOnError="true" />


Only Digit Numeric Value Allowed
ValidationExpression="^\d+$"

<asp:RegularExpressionValidator ID="revPinCode" runat="server" ErrorMessage="Please enter valid pin code" ControlToValidate="txtpincode" ValidationGroup="valpartcipate" ValidationExpression="^\d+$"
Display="None" SetFocusOnError="true" />
Read More »

My Blog List