alert if the user didn't fill the required fields - asp.net

I have a field in asp and he has RequiredFieldValidator.
I have also a button that on click I send mail to the user.
my code:
<asp:TextBox class="form-control" ID="mail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="ShemPrati"
ErrorMessage="you must enter your mail"
Display="Dynamic"
ForeColor="Red"> </asp:RequiredFieldValidator>
<asp:Button ID="Button4" class="btn btn-default" runat="server" Text="send mail" OnClick="Button4_Click" />
When I click on the button it's give me the ErrorMessage (if the field is empty).
I want also to alert a massage that he will be notify that there are a required fields in the form.
How can I do it?
thanks!

You can achieve your requirement by adding a client-side onclick event to the button that submits the form:
<script language="javascript" type="text/javascript">
function checkIfFormIsValid() {
//this will check if the form is valid
if (Page_IsValid == false) {
alert('Your form is not valid!');
}
}
</script>
Then you can call the function when someone clicks your button:
<asp:Button ID="Button4" OnClientClick="JAVASCRIPT:checkIfFormIsValid();" class="btn btn-default" runat="server" Text="send mail" OnClick="Button4_Click" />
BONUS:
You can add an onblur client-side event to a specific text box and do some custom stuff that way:
txtMail.Attributes.Add("onblur","javascript:checkIfFormIsValid();");

Related

How to fix validation summary message not getting refreshed when keep SetFocusOnError as true

I am using two asp.net validation controls 1. RequiredFieldValidator & 2. RegularExpressionValidator for my password textbox to validate the user input and set its display property to none so that valiadation message will only get displayed in validationsummary control.
when I click on submit button the validation get displayed as expected but when I use SetFocusOnError as true for both validators sometimes the validation message doesn't get refreshed for ex. if there is no text entered in the text box and click on submit button it display required field validation message but when enter any text and click on submit button it still display required field validation instead of RegularExpressionValidator validation message but when click submit button next time it propertly display
RegularExpressionValidator validation message
<asp:TextBox ID="txtPass" CssClass="form-control" runat="server" TextMode="Password"
meta:resourcekey="txtPassResource1" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPass" runat="server" SetFocusOnError="true" ControlToValidate="txtPass"
Display="None" ErrorMessage="Enter Password" ValidationGroup="submit"
meta:resourcekey="rfvPassResource1"></asp:RequiredFieldValidator>
<div class="password_error" style="width:auto;">
<asp:RegularExpressionValidator ID="rxvalPassword" runat="server" ErrorMessage="Password should be at least 6 characters in length. It should have at least one numeric and one special character from ~ ! # # $ & _ . * "
ControlToValidate="txtPass" SetFocusOnError="true" ValidationExpression="^(?=.*[0-9])(?=.*[~!##$&_.*])[A-Za-z0-9~!##$&_.*]{6,15}$"
ValidationGroup="submit" Display="none" meta:resourcekey="SpecialCharResource2" />
</div>
<div class="rows col-md-offset-5">
<div class="width12">
<asp:ValidationSummary ID="valnewuser" runat="server" ValidationGroup="submit" meta:resourcekey="valnewuserResource1" EnableClientScript="True" />
</div>
</div>
<div class="col-md-offset-6">
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return ValidatePage();" CssClass="btn btn-primary" Text="Submit" OnClick="btnSubmit_Click"
ValidationGroup="submit" meta:resourcekey="btnSubmitResource1" />
</div>
Please do test it again as when I reviewed and tested your code, it worked perfectly fine.
The error showed as per validation. If still some anomaly happens, do show that exact code.

HTML 5 required validator triggers on all buttons on the form

<asp:TextBox ID="txtCompanyName" runat="server" placeholder="Company Name" BorderColor="#C2C4CC" BorderStyle="Solid" Height="28px" Width="135px" title="Enter Company Name" BackColor="#F9E3CB" Enabled="False" TabIndex="7" required></asp:TextBox>
Here is the mark up for one of my textbox with the "required" attribute.
My question is, I have 3 buttons on my form, cancel, reset and submit button.
the html5 required validator should only trigger when the submit button is clicked.
however, it still triggers even if I press cancel and reset buttons.
How can I assign html 5 required validator on only one button?
here is my markups for my buttons:
Cancel Button:
<asp:ImageButton ID="Cancel" runat="server" ImageUrl="~/Images/ClaimForm/btnCancel.gif" OnClick="btnFallBack_Click" Visible="False" ToolTip="Cancel" draggable="false" />
Reset Button:
<asp:ImageButton ID="btnClear" runat="server" OnClick = "OnConfirm" OnClientClick = "Confirm()" ImageUrl="~/Images/ClaimForm/Clear.jpg" draggable="false"/>
Submit Button
<asp:ImageButton ID="btnNext" runat="server" ImageUrl="~/Images/ClaimForm/submitbutton.jpg" draggable="false" OnClick="btnNext_Click"/>
Add formnovalidate tag in each buttons html code.
<asp:ImageButton ID="Cancel" runat="server" ImageUrl="~/Images/ClaimForm/btnCancel.gif" OnClick="btnFallBack_Click" Visible="False" ToolTip="Cancel" draggable="false" formnovalidate="formnovalidate" />
I solved it by assigning an onclick function to that button and adding:
event.preventDefault();
try this.
use a input button - where set the type as button and make it to run as server
ex:
// this will trigger the required for text boxes
//this button will not.

How to use client side scripts with customValidators?

If I leave the checkbox blank and click the submit button, nothing happens. If I check it and hit the submit button, nothing happens.
The javascript fires, I just don't know why no text is displayed and the page doesn't postback.
function ValidateCheckBox(source, args) {
args.IsValid = document.getElementById('<%= cb_confirmation.ClientID%>');
}
Here is the validator:
<asp:CustomValidator runat="server" ID="cb_Validator"
OnServerValidate="CheckBoxRequired_ServerValidate"
CssClass="errorMessage" Display="Static" ErrorMessage="You must select this box to confirm"
Text="***" ClientValidationFunction="ValidateCheckBox">
</asp:CustomValidator>
The checkbox:
<asp:CheckBox ID="cb_confirmation" runat="server" />
And the button that causes validation is
<asp:Button ID="btn_submit" runat="server" Text="Submit" CausesValidation="true"
CssClass="submitButton" onclick="btn_submit_Click"/>
EDIT: The solution was to add a ".checked" to the end of the javascript function.

input button causes RequiredFieldValidator to fire

I have an input button on my page that is dynamically created, every time I press it, it fires the RequiredFieldValidator for the blank field in the email address.
is there a way to ovverride it?
input code:
<input class=\"hledat\" id=\"searchbutton\" type=\"image\" src=\"search-button.gif\" value=\"{0}\" onclick=\"search('{1}');\" onkeypress=\"search('{1}');\"/>"
validator code:
<asp:TextBox runat="server" ID="txtEmail" ClientIDMode="Static" Width="98%" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="email"
Display="dynamic" ValidationGroup="newsletter" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="email"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="dynamic"
ValidationGroup="newsletter" />
I have an input button on my page that is dynamically created, every
time I press it, it fires the RequiredFieldValidator for the blank
field in the email address. is there a way to ovverride it?
HtmlButton.CausesValidation Property
Mark UP
<button causesvalidation="false" /><button ID="StateQueryButton"
CausesValidation="False"
runat="server">
Submit
</button>
yes, i want it to do a postback
HtmlButton.OnServerClick Method
Code Behind
protected void FancyBtn_Click(object sender, EventArgs e)
{
}
Mark Up
<button causesvalidation="false" /><button ID="StateQueryButton"
CausesValidation="False"
OnServerClick=" FancyBtn_Click"
runat="server" >
Submit
</button>
Make it server side button by writing runat="Server" attribute and make a CausesValidation="true|false" on your requirements. If you do false it will not validate. Do let me know if it solves the problem
try to add runat="server" to input button as shown:
*Note* the runat="server". While asp:button probably renders similarly, if what you really want it an HTML button input, you can use that. Yes, ASP.NET will pick up the value on the server side.**
<input type="button" runat="server" id="btnTest" value="change" onclick="doPostBack('btnTest', '');"/>

ASP.NET Enter key triggering unwanted button

I have this on the top of a page (in MasterPage.master)
<asp:Panel ID="panSearch" runat="server">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="~/images/iconSearch.gif" onclick="btnSearch_Click" />
</asp:Panel>
The button keeps getting fired when I press the enter key on another TextBox down the page (in an aspx page)
<input type="text" id="txtTagName">
<input type="button" value="Tag" id="btnAddTagOk">
I couldn't find any JavaScript that does that.
Anyone has any idea why it's happening?
Hitting enter will submit the default form. If you have several controls which you want control over the enter button you need to hook in javascript on the control to intercept the enter key press, and then do your own logic.
Something like this
<input type="text" id="txtTagName" onkeydown="if (event.keyCode == 13) document.getElementById('btnAddTagOk').click()"/>
you might have some other button with its type as type="submit". The browser by default submits the form when enter is pressed on textboxes.
you might want to try following on your asp:panel
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch">
</asp:Panel>

Resources