In my Content page I have 4 different TextBox's which is used to enter phone number - Mobile, Office Phone, Office Mobile & Other. I have RegularExpressionValidator for each.
I want to make sure atleast one of the above 4 textbox's has entered a value. I didn't find any example or article on net showing this situation. One way I think is in Submit button click, before Page.IsValid call, call a function that checks that one of the control has valid value. If the function returns false, show MessageBox. Is their any other way using Validators or so to comfirm that one textbox out of 4 has a valid value.
What can be the best way to achieve this ?
Any help is highly appreciated.
Thankss
You can create a Custom Validator in ASP.NET.
This post should help you out.
asp.net required field validator for at least one textbox contains text
Related
ASP.NET WebForms 4.51
Having read things like How to validate two groups of controls with 1 button and a validation group?
I have a form with the following logical inputs:
Name (TextBox)
Last Name (TextBox)
Profession (Drop Down List)
Profession has the following items:
Bus Driver
Taxi Driver
Not Applicable
Now based on the profession I display a number of other text fields. I have required field validators on all the fields 1..3 as well as the fields for the profession (4..8).
I have a single submit button which triggers the validation. So my question is what is the easiest way to validate controls: 1..3 should the selection be Not Applicable for profession and 1..8 for the other professions from a single button.
I can think of a couple of ways but they are all "messy" and I was looking for a better approach:
1) Have two required field validators per field - one when validating for Not Applicable and one for when a profession is selected. Toggle the validation group when the profession is selected for the button.
I tried this but it does not seem to work as I still get both validators firing per field.
2) Write custom client side validators that take into account what is selected in the drop down for profession.
A maintenance nightmare. I would prefer to use any built in functionality MS provides.
So has anyone got any suggestions on how to validate the same sets of controls differently based on what has been selected in another control?
You could write a simple javascript validation script. You can not validate the javascript on serverside using "page.Isvalid".
Your only option is to write a custom validator. I just don't know why that should be a maintenance nightmare. If you're using the javascript validation you should validate the values in the code behind anyway. Otherwise the user could just disable javascript and enter anything he wants.
Hope it helps...
I have a form that asks a user for their first name, last name, phone number, ID number, username, and check boxes to choose either faculty or student in that order. The first two text boxes have two validators each, a regular expression: ^[a-zA-Z- ]*$ and required field validator. After that I created some custom validators as in addition to the text box for phone number, ID number, and username I have a "Don't know?" checkbox in case they forgot. So it validates to make sure they either typed in something and if not to make sure the check box is checked. Now, if I fire up the site and simply click the confirm button only the first two fields trigger their validation, that is the first and last name fields. Now, if I put in a first and last name and click confirm button again the rest of the validators trigger as normal. How can I get them to all trigger at once? Thanks.
Your custom validator logic may be implemented only in server side. You may have to provide the validation logic at client side by writing a javascript method and specifying it in ClientValidationFunction property. The link also provides an example on how to implement it in the client side.
I am writing in asp.net c#.
I want a control similiar to RequiredFieldValidator except I want one of two fields to be required. I found an excellent example for two text fields but in my case one field is a check box and the other is an text box. If the check box is not checked the text box must be entered.
Any thoughts?
Just use javascript or C# code to check this.
I personally don't care for the RequiredFieldValidator types as they are limited and rather confusing.
With C# server side code you could just check
if (!chk.Checked && txtBox.Text.Length==0)
For JavaScript something to this effect:
if (!(document.getElementById('myCheckBox').Checked)
&& document.getElementByID('myTextBox').val=='')
You can always use the customvalidator if you have complex custom validation logic. This is probably your best way to go.
There is another relevant question asked Validation Check in asp.net
In the same scenario we need a custom validator control which will alert user for any wrong entry. This will work like this :
Developer will pass the control-name, input-value and format-required
For instance like for textbox it can be: txtName,txtName.Text, allow-alphabets-only
The accordingly format if the user input is invalid he/she will be got prompt.
Please suggest the right way to do the smae.
Thanks in advance.
You've described the default behaviour of all asp.net validators (pass control to validate etc).
What you want to achive (eg, alpha charaters only, or alphanumeric only etc) can be achieved mostly with the RegularExpressionValidator.
I'm working on an ASP.NET/C# app.
I have 2 text boxes and I need to validate if the input text for both of them are not both null
ex:
if(string.IsNullOrEmpty(TextBox1.Text) && string.IsNullOrEmpty(TextBox2.Text) )
//FAIL!!!
else
// OK!
that is, at least one txtBox has values
I was planning on using a custom validator but it seems that the validation function only gets called when something is written on the textBox i'm using has the 'control to validate'. Now, that doesn't work for me since I want to show an error message when both text boxes are empty. Is there a way to make the validation function to be called with, for example a postback? Or is there any other better approach to this case than the custom validator?
tks
If you set the 'ControlToValidate', then the validator will not fire if that control is empty. However, for a CustomValidator you can leave that empty so it will always fire.
Because the built-in validators only analyze the state of their own controls, you are forced to go with a CustomValidator for this functionality. I don't know of any better approach that still uses ASP.NET server-side validators. As Hans said, be sure to leave ControlToValidate empty, and then do the validation as in your code sample.