I want use a RegularExpressionValidator for a date in this form yyyy-mm-dd (example: 2012-11-29) and here is my expresion:
/^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$/
I test it on http://www.quanetic.com/Regex and it works but if I do this in my asp.net application it doesn't work
<tr>
<td>Gültig ab:</td>
<td><asp:TextBox ID="txtVon" runat="server" ></asp:TextBox></td>
<td><asp:ImageButton ID="imgVon" runat="server" ImageUrl="images/Calender.ico" Width="15" Height="15" />
<asp:CalendarExtender runat="server" ID="E_Von" TargetControlID="txtVon" Format="yyyy-MM-dd" PopupButtonID="imgVon"/></td>
<td>
<asp:RequiredFieldValidator ID="ValVon"
runat="server" ForeColor="red"
ErrorMessage="*" ControlToValidate="txtVon"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regVon"
runat="server" ControlToValidate="txtVon"
ErrorMessage="*Format" ForeColor="red"
ValidationExpression="/^(19[789]\d|20[0123]\d)\-(0\d|1[012]|\d)\-(31|30|[012]\d|\d)$/"></asp:RegularExpressionValidator>
</td>
</tr>
Where is the error?
Just remove char "/" in the begining and in the and of the string.
And you will have
ValidationExpression="^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$"
I use the following, which works ok.
\A(?:^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$)\Z
Related
How to check that entered date is less than or not in 22nd of every month in a calendar, if I choose 23 then give me an error message in asp.net
<tr>
<td>Select Date:</td>
<td> </td>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:TextBox ID="txtdate" runat="server" CssClass="form-control" placeholder="yyyy-mm-dd" ForeColor="Black" required="true"></asp:TextBox>
<asp:CalendarExtender ID="CalExt" runat="server" CssClass="cal_Theme1" TargetControlID="txtdate" PopupButtonID="txtdate" Format="yyyy-MM-dd" />
</td>
<td> </td>
<td> </td>
</tr>
This is my front-end code and please help to how to solve this
You might be looking for:
<ajaxToolkit:CalendarExtender ID="CalExt" runat="server" CssClass="cal_Theme1" TargetControlID="txtdate" PopupButtonID="txtdate" Format="yyyy-MM-dd" />
Instead of "<asp:CalendarExtender"
After you make that change, you can work out how to validate the date the user selects, which may require you to write a custom validator.
I have used compare validator to check whether the selected date is valid or it. The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.
<tr id="trow" runat="server">
<td class="auto-style3">Need Duration</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
It has been a while, but I think you need to enable client side validation scripts by adding:
EnableClientScript="True"
Example
<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server"
Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
It's documented at msdn.
Aditionally, I do know that custom validators often lack a correct implementation of the javascript. I am not sure how the CompareValidatorbehaves in that sense.
You might need to create a inherited class, to implement the scripts fully. Before going there, try do research a bit.
For example, here is a solution with a custom validator
I'm using two contact forms in the same aspx page, one has 4 textbox controls (name- email- subject- body) and send button control.
The second form has 2 textbox controls and send button, all (except the second textbox of the second form) has validation controls (requiredFieldValidator).
This is the code inside the first button click event:
protected void ButtonSend_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
LabelIRespond.Visible = true;
.....
}
}
there is no code in the second button click event.
the problem is, when the first form validated successful and the second form not validated, the code inside click event not executed. and the error message of the second form appears. They act like they are one form!
The markup for both forms:
<section id="sendSection">
<div id="contactMe">
<h1>Contact me</h1>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<h4>Send me a message:</h4>
<table class="auto-style1">
<tr>
<td class="auto-style8">
<asp:TextBox ID="TextBoxEmail" placeholder="Your Email address" runat="server" BackColor="#D4D8DF" Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ControlToValidate="TextBoxEmail" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Please enter your Email address">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBoxEmail" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style8">
<asp:TextBox ID="TextBoxName" placeholder="Your name" runat="server" BackColor="#D4D8DF" Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="TextBoxName" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style5">
<asp:TextBox ID="TextBoxSubject" placeholder="Subject" runat="server" BackColor="#D4D8DF" Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
</td>
<td class="auto-style3">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorSubject" runat="server" ControlToValidate="TextBoxSubject" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style6">
<asp:TextBox ID="TextBoxBody" placeholder="What do you want to tell me?" runat="server" BackColor="#D4D8DF" TextMode="MultiLine" Font-Names="Century Gothic" CssClass="txtbxs" Height="180px" Width="370px"></asp:TextBox>
</td>
<td class="auto-style2">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorBody" runat="server" ControlToValidate="TextBoxBody" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Type your message">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style8">
<asp:Button ID="ButtonSend" runat="server" Text="Send" CssClass="btnSend" Width="80px" OnClick="ButtonSend_Click" UseSubmitBehavior="False" Font-Names="Century Gothic" />
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style8">
<asp:Label ID="LabelIRespond" runat="server" Text="Thanks, your message has been sent. I'll respond ASAP." Visible="False"></asp:Label>
</td>
<td>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="wait">
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</section>
<div class="split"></div>
<section id="viewCVSection">
<div id="check">
<h1>Check out my CV</h1>
</div>
<div class="cvForm">
<table class="auto-style1">
<tr>
<td class="auto-style4">
<asp:TextBox ID="TextBoxEmail2" placeholder="Company Email address" CssClass="txtbxs" runat="server" BackColor="#D4D8DF" Height="25px" Width="250px" Font-Names="Century Gothic"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxEmail2" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBoxEmail2" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:TextBox ID="TextBoxCompany" placeholder="Company name" CssClass="txtbxs" runat="server" BackColor="#D4D8DF" Height="25px" Width="250px" Font-Names="Century Gothic"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Button ID="Button1" CssClass="sendCV" runat="server" Text="Request CV" Font-Names="Century Gothic" OnClick="Button1_Click" />
</td>
<td> </td>
</tr>
</table>
</div>
</section>
You need to hook up validation controls with the correct button. Right now, button click in first form is causing validation in first contact form as well as second contact form, which is causing Page.IsValid to be false since second form fields are empty and therefore invalid.
So set the validation group of first contact form different from the second contact form as in code below. Then set the ValidationGroup on corresponding validation controls with the same value.
<asp:Button ID="ButtonSend" runat="server" Text="Send" CssClass="btnSend"
Width="80px" OnClick="ButtonSend_Click" UseSubmitBehavior="False"
Font-Names="Century Gothic" ValidationGroup="FirstGroup"/>
<asp:Button ID="Button1" CssClass="sendCV" runat="server"
Text="Request CV" Font-Names="Century Gothic" OnClick="Button1_Click"
ValidationGroup="SecondGroup"/>
NOTE: Make sure that all validation controls have their ValidationGroup also set to appropriate value after you make above code change.
Thank you very much for taking the time to read this. I have an issue in which I was setting a default button for an ASP.NET page on page load from code behind, but now that I have multiple validation groups targeting one control, that is no longer working. Now, when I hit enter while in that control (textbox), validation for both groups are triggered minus the validation summary text.
Here is my examplefied code:
ASPX
<table>
<tr>
<td><asp:Textbox runat="server" ID="validateMe"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
<td><asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</tr>
<tr>
<td><asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup"/></td>
<td><asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup"/></td>
</tr>
<table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup"/>
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = firstButton.UniqueID;
}
If I use this and hit 'Enter' while inside of the textbox without typing anything into it, then neither of the validation summaries will appear but I will have two asterisks next to the textbox (one for each group). If the user presses 'Enter' I would expect a full validation using only the first group which is supposed to be assigned to the DefaultButton (here, 'firstButton'). Is there any way to achieve this functionality and initiate the client-side validation that would have happened had the user clicked 'firstButton' instead?
I have also tried wrapping the whole table plus the validation summaries inside of an asp:Panel and setting the DefaultButton there, but I received the same results. Any help or pointers in the right direction would be greatly appreciated!
Set
EnableClientScript="false"
in RequiredFieldValidator control. It will help.
<asp:Panel runat="server" DefaultButton="secondButton">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="validateMe"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup" /></td>
<td>
<asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup" /></td>
</tr>
</table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup" />
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup" />
</asp:Panel>
I have a captcha control, when write code and check it it always be wrong if I write it correctly , then form the second time it works fine.
what may be the problem in that
that is the captcha
<td id="Td6" runat="server" align="center" class="style4">
<MSCapatchaControl:CaptchaControl ID="Capatcha"
runat="server"
BackColor="#CC3300"
CaptchaBackgroundNoise="Extreme"
CaptchaChars="ACDEFGHJKLNPQRTUVXYZ2346789"
CaptchaHeight="40"
CaptchaMaxTimeout="240"
CaptchaMinTimeout="5"
CaptchaWidth="130"
FontColor="White"
ForeColor="White"
LineColor="Black"
NoiseColor="Black" />
</td>
<td id="Td7" runat="server" colspan="3">
<asp:TextBox ID="registerCaptchText"
runat="server" MaxLength="6" Width="135px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="registerCaptchText"
Display="None"
ErrorMessage="Enter the capatcha">
</asp:RequiredFieldValidator>
<font style="font-size:12px; font-family:trebuchet; color: rgb(51, 51, 51);">
<div class="important">*</div>
Please enter the text you see in the image.
</font>
</td>
and that is the function test it
Capatcha.ValidateCaptcha(registerCaptchText.Text);
bool capatchaValid = Capatcha.UserValidated;
Hi I have solved this problem by taken captcha field in update panel.
Thanks Every one for your value able time.