0

I have asp.net form with text boxes and drop downs. I do client side validation for these controls on a button click.

For example, to validate the below firstname textbox, I am using jquery to check for no input and valid characters. If textbox is empty first <span> will be displayed. If invalid characters are typed second <span> will be displayed. Now on the button click I want to get the error message shown (anyone one ).

<asp:TextBox ID="firstNameTxt" runat="server"></asp:TextBox>
<br />
<span class="validation validation-checkEmpty">Please enter your first name.</span>
<span class="validation validation-checkValidChars">We can only accept letters, apostrophes, and hyphens for your first name.</span>

I have to do this for all textboxes, drop downs, checkboxes in the form.

So my question is how to get the error message shown in <span> when the validation fails. I have to do this for all controls in the form.

Can anyone guide me in the right way.

Thanks,

Arul

Manoj
  • 1
  • 2

3 Answers3

1

You need to give id in your spa tag, and write a javascrpt method o ontextchange event of thext bosex.then depending on your input, you should hide/show your correspondin span tag.

Here is an example : how to hide a <span> tag and show it with javascript

Community
  • 1
  • 1
Badhon Ashfaq
  • 851
  • 6
  • 9
  • Thanks for reply. Acutally I have already done with the validation part. Now I have to send all error messages to google analytics for each input which has validation failed. – Manoj May 16 '14 at 06:35
0
<form action="/registration" method="POST">
 <p>
  Name (4 characters minimum):
  <input name="user" data-validation="length" data-validation-length="min4">
 </p>
 <p>
  Year (yyyy-mm-dd):
  <input name="birth" data-validation="date" data-validation-format="yyyy-mm-dd">
 </p>
 <p>
  Website:
  <input name="website" data-validation="url">
 </p>
 <p>
  <input type="submit">
 </p>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.1.47/jquery.form-validator.min.js"></script>
<script> $.validate(); </script>

You can take a look at demo on, http://jqueryvalidation.org/documentation/

Afzal Ahmad
  • 586
  • 5
  • 20
0

I would use a validation function like this: function validateEntries() { var err = 0; var errmsg = "Please fix the following errors:"; if ($.("#" + ").val() == '') { err = 1; errmsg = errmsg + "\n" + "First name is missing"; } if (err > 0) { alert (errmsg); return false; } return true; }

Dean.DePue
  • 1,013
  • 1
  • 21
  • 45