Results 1 to 10 of 10

Thread: RegularExpressionValidator and form submission

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    RegularExpressionValidator and form submission

    I have a form that has some fields that I want to validate using regularexpressionvalidator and requiredfieldvalidator. I dont want the form to submit if any of the validations fail. However I am having trouble with the RegularExpressionValidator if I put any values in there the form submits, even if a) it doent fit into the validation i put in
    and b) the other required fields are not even filled in.
    Can someone help me with this?
    here is my code:

    Code:
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="JobInfo.aspx.vb" Inherits="AjaxUploader.JobInfo1" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxToolkit" %>
    
    <%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
    
    <!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">
    <script language="javascript">
    function FillHidPhone(){
        document.forms[0].hidPhone.value=document.forms[0].txtPhone1.value + document.forms[0].txtPhone2.value + document.forms[0].txtPhone3.value;
        //alert(document.forms[0].hidPhone.value);
    }
    
    </script>
        <title>Job Information</title>
     </head>
    <body>
    <form id="Form1" runat="server">
    <asp:TextBox name="hidPhone" id="hidPhone" runat="server"/>
    <table width="65%" border="0">
    <tbody>
    	<tr>
    		<td>*Contact</div></td>
    		<td><asp:TextBox ID="txtContact" runat="server" CssClass=""></asp:TextBox>
    		<asp:RequiredFieldValidator ID="req1" runat="server" ControlToValidate="txtContact" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator></td>
    	</tr>
    	<tr>
    		<td>*Phone</td>
    		<td><asp:TextBox ID="txtPhone1" onChange ="FillHidPhone()" runat="server" CssClass="" MaxLength="3" Width="30"></asp:TextBox>
    			-
    			<asp:TextBox ID="txtPhone2" onChange ="FillHidPhone()" runat="server" CssClass="" MaxLength="3" Width="30"></asp:TextBox>
    			-
    			<asp:TextBox ID="txtPhone3" onChange ="FillHidPhone()" runat="server" CssClass="" MaxLength="4" Width="35"></asp:TextBox>
    		 <asp:RequiredFieldValidator ID="Req2" runat="server" ControlToValidate="hidPhone" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegExp" runat="server" ControlToValidate="hidPhone" ErrorMessage="Incorrect format" ValidationExpression="^[0-9]*${10}" Display="Dynamic"></asp:RegularExpressionValidator>
       	            </td>
    		<td>&nbsp;
    		    </td>
       	</tr>
    	<tr>
    		<td>Mobile</td>
    		<td><asp:TextBox ID="txtMobile1" runat="server" CssClass="" MaxLength="3" Width="30"></asp:TextBox>
    			-
    			<asp:TextBox ID="txtMobile2" runat="server" CssClass="" MaxLength="3" Width="30"></asp:TextBox>
    			-
    			<asp:TextBox ID="txtMobile3" runat="server" CssClass="" MaxLength="4" Width="35"></asp:TextBox></td>
    		<td>&nbsp;</td>
    	</tr>
    	<tr>
    		<td>*Email Address</td>
    		<td><asp:TextBox ID="txtEmail" runat="server" CssClass=""></asp:TextBox>
           	<asp:RequiredFieldValidator ID="Req3" runat="server" ControlToValidate="txtEmail" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="Reg2" runat="server" ControlToValidate="txtEmail" ErrorMessage="Incorrect format" ValidationExpression="\S+@\S+\.\S{2,3}" Display="Dynamic"></asp:RegularExpressionValidator>
           </td>
    	</tr>
    	<tr>
    		<td>Mailing Address:</td>
    		<td><asp:TextBox ID="txtAddress" runat="server" CssClass=""></asp:TextBox></td>
    	</tr>
    	<tr>
    		<td>*Project / Property Name</td>
    		<td><asp:TextBox ID="txtProject" runat="server" CssClass=""></asp:TextBox>
        	 <asp:RequiredFieldValidator ID="Req4" runat="server" ControlToValidate="txtProject" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator>
    		</td>
    	</tr>
    	<tr>
    		<td>*Number of Leases</td>
    		<td><asp:TextBox ID="txtLeaseAmount" runat="server" CssClass="" MaxLength="3"></asp:TextBox>
        	 <asp:RequiredFieldValidator ID="Req5" runat="server" ControlToValidate="txtLeaseAmount" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator>
    		</td>
    	</tr>
    	<tr>
    		<td>*Deadline</td>
    		<td><asp:Textbox id="txtDueDate" runat="server" ></asp:textbox>
        	 <asp:RequiredFieldValidator ID="Req6" runat="server" ControlToValidate="txtDueDate" ErrorMessage="Required" Display="Dynamic"></asp:RequiredFieldValidator>
            <AjaxToolkit:calendarextender id="CalExtender" runat="server" targetcontrolid="txtDueDate" Format="MM/dd/yyyy" enabled="true"></AjaxToolkit:calendarextender>
            </td>
    	</tr>
    	<tr>
    	    <td><asp:Button Width="106" Height="26" ID="btnContinue" Text="Continue" runat="server"/></td>
    	</tr>
    	</tbody>
    </table>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    
    </form>
    <script language="javascript">
    document.forms[0].hidPhone.style.visibility = "hidden";
    </script>
    </body>
    </html>

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: RegularExpressionValidator and form submission

    Is this right?
    ^[0-9]*${10}

    It means "I want a number, 0 or more times, 10 times". So that should match up with a blank string, shouldn't it?

    Try

    [0-9]{10}

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: RegularExpressionValidator and form submission

    I tried that but nothing seemed to change. any other ideas what the problem might be?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: RegularExpressionValidator and form submission

    Do you, anywhere in markup or code, change the causesvalidation property of the button? The button's CausesValidation property should be set to true.

  5. #5
    Hyperactive Member dnanetwork's Avatar
    Join Date
    Oct 2007
    Location
    Mumbai
    Posts
    349

    Re: RegularExpressionValidator and form submission

    try this one..

    [0-9]*

    in your regular expression..

    and if this part is not working then...

    i 'd like you to check your control who's causing a postback..

    if yes then...you need to disable their causeValidation property = false.

    and enable cause validation in your continue buttonn...

    hope it helps..

    after that u getting the error then..

    show me your aspx code and code behind/...

    may be i can help...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: RegularExpressionValidator and form submission

    The "causeValidation" property is True.
    I tried changing the regular expression as you described and I am still running into the same strange problem. Again what happens is, if the fields are empty then it wont submit, as soon as I put any text into the box of the "RegularExpressionValidator" then the form submits even though the other fields are still empty.
    I already pasted my code in the original post.
    Any help with this would be appreciated.
    Thanks.

  7. #7
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: RegularExpressionValidator and form submission

    Strange problem - maybe strange markup.

    You have javascript events firing onChange ="FillHidPhone()" on many text boxes that change the textbox your refering to "hidPhone" like document.forms[0].hidPhone....... and there is another javascript function refering to hidPhone at the bottom of the page and your wanting to use required & regex validators on it aswell.....

    What are you trying to achieve ?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    Re: RegularExpressionValidator and form submission

    I have 3 text boxes for the phone number. I want to do a validation check on the entire phone number (all three textboxes) someone gave me the idea of putting all the textboxes together into one hidden texbox and then validating the hidden textbox. do you think the javascript could be what is causing the problem?

  9. #9
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: RegularExpressionValidator and form submission

    Now I see what your trying to achieve. Without testing it I'd say the javascript will be the problem, as well your regex expression is not correct.

    You may be able to find a combination of JS and validation controls that works. Personally because what your doing is "outside the box" or specalized I'd just use javascript.

    The best/simplest solution could be to use 1 textbox (not 3) a required & regex validator.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: RegularExpressionValidator and form submission

    Your form is starting to make sense now, but is unnecessarily complicated. You can go for 1 textbox with a phone regex, or if you stick to three textboxes, then validate them together using a custom validator. A custom validator control lets you specify a javascript method that you can call when validating client side, as well as a server side validating method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width