How to disallow use of double quotes " in a textbox
Use String.Contains()
Since you can never control what users do the client you might as well just check it on the server, so you would probably use code something like:
if (textBox1.Text.Contains("\""))
{
Response.Write("Dey aint no way I'm letting you type that!");
}
use RegularExpressionValidator and set the ValidationExpression='^[^\"]*$', that will allow anything, including empty, other than "
If you don't want users to be able to type quotes in the textbox in the first place, consider using a FilteredTextBox from the AJAX Control Toolkit.
Use the RegularExpressionValidator for your page input validation. The .NET validation controls will verify your users' input on both sides, the client-side, and the server-side which is important if the user has disabled JavaScript. This article might also help you to implement the validation of your ASP.NET server controls.
Please don't do this just with JavaScript or AJAX. Always perform a server-side input validation! Especially if you are writing the users' input back into a database (SQL Injections).
You haven't specified if you're using webforms or MVC, so I'm gonna throw a couple things a'cha.
First off, here's the Regex you'll use in either situation. ^[^\"]*$
First for WebForms
<asp:TextBox runat="server" id="TextBox1" />
<asp:RegularExpressionValidator runat="server" id="Regex1" controltovalidate="TextBox1" validationexpression="^[^\"]*$" errormessage="Nope!" />
<!-- This will give you client AND server side validation capabilities-->
To ensure you're valid on the SERVER SIDE, you add this to your form submit method
If Page.IsValid Then
''# submit the form
Else
''# your form was not entered properly.
''# Even if the user disables Javascript, we're gonna catch them here
End If
In MVC you should definitely use DataAnnotations on your ViewModel
NOTE: DataAnnotations can also be use for WebForms if you're planning on doing a lot of repeat ctrl + C and ctrl + V
''# fix SO code coloring
''# this is in the view model
<RegularExpression("^[^\"]*$", ErrorMessage:="Nope")>
Public Property TextBox1 As String ''# don't actually call it TextBox1 of course
And in your controller "Post" action, you wanna add the following
If ModelState.IsValid Then
''# submit the form
Else
''# your form was not entered properly.
''# Even if the user disables Javascript, we're gonna catch them here
End If
Related
I have a page with a repeater containing RadioButtonLists which have requiredFieldValidators attached to them. I need to keep the RFV next to the control (it's the only way I can get it to work to be honest!)
However, the form is made up of a few sections contained in an accordion. This means that when the form is submitted, the item that has failed validation may not be visible, so the user won't know where the error is.
Is there a way I can also have a message by the submit button which is triggered by an RFV changing saying "please go back and check your answers" or something? I guess I'd need to use JQuery / JavaScript as it would be clientside.
There is a special ValidationSummary control for that:
<asp:ValidationSummary ID="Summary" runat="server"
DisplayMode="SingleParagraph"
HeaderText="Please go back and check your answers" />
This control is used to summarize all validation errors on the page.
Try "ValidationSummary". look for example from here.
http://www.w3schools.com/aspnet/control_validationsummary.asp
im working on an .aspx page in Visual Studio.
I want to have a text box that is followed by a drop down menu.
if the user enters any input in the text box id like for it and the drop down menu to both be required before the corresponding button can be clicked.
is the best way to do this to use a RequiredFieldValidator ?
I think what you attempt to do is Conditional Validation
This question is similar to your question for Conditional Validation ASP.NET
Yes, RequiredFieldValidator would work well for your scenario. Just be sure to enable it or disable based on 'if the user enters any input in the text box'
You can create validators for both fields and onblur of the textbox enable/disable the validators using javascript.
HTML:
<asp:TextBox runat="server" ID="txt" onblur="enableVaidators();" />
Javascript:
function enableValidators()
{
var val_Test = document.getElementById('<%=val_Test.ClientID%>');
var enableValidators = true;
// Perform check on whether to enable or disable based on your scenario
ValidatorEnable(val_Test, enableValidators);
}
how about using jquery?
everything is done on the client-side:
http://docs.jquery.com/Plugins/Validation/
I would use a CustomValidator which implemented logic based on the state of the TextBox.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I am having some textboxes and dropdownlist controls but if i select the ddl value the validation errors are disappearing and after the button click they are reappearing but i want to show the errors even after a postback how can i do this??
Could you post your code so we can perhaps see what the specific issue might be? Without seeing any code, I would say try adding this into the Page_Load function:
if (IsPostBack)
Page.Validate();
Alternatively, add this to your DropDownList or whatever controls are initiating the postback:
CausesValidation="true"
.. as per the answer here: Validators do not Validate after postback occurs
Please remove your asp.net validators if added with the controls and also remove any client side validation in Java script. Now add validation code on the page you are redirecting to. If that validation fails redirect back to the controls page with proper messages to be shown
If you dont like the default behavior - don't use validation controls and implement them by yourself using client side programming.
I need to call a validator to show an error message from JavaScript
For Example:
I have a textbox in which i take time as input and a Button to submit the value.
I have a minimum time and maximum time.
I'm validating the time textbox using JavaScript and using an alert message to show if there is an error in the time entered
Now instead of showing the alert box I want to call the validation callout extender
to show the error message
Can you help me out?
Have you tried using ASPX validators, these solve javascript and also server side parts.
You can then just ask page.IsValid.
here is some more information about ASPX validator controls
Validators are pretty easy to use, as they support also regular expressions and own functions. But sometimes you've to use two validators on one field. e.g. requiredFieldValidator and Compare validator
http://asp.net-tutorials.com/validation/required-field-validator/
http://msdn.microsoft.com/en-us/library/bwd43d0x.aspx
http://msdn.microsoft.com/en-us/library/debza5t0.aspx
I have 10 defined textboxes with strings.
I have to check all if they are not empty while clicking ok button
whats the cleanest way to check them all and when function is at end. each checkbox which was empty to give this a specific CSSclass. perhaps. ClassError. ( which highlights red)
I'm happy for answers.
I would add RequiredFieldValidators to them, as well as a ValidationSummary control.
edit: You can also add fancy AJAX effects with the ValidatorCallout from the AJAX toolkit.
edit: Validator controls also support client-side validation.
Using javascript or C#?
Javascript I will create an array of textbox and loop through it.
C# just go FindControl within a Panel or the Container of the text box and go something like this
foreach(Control C in ContainerID.Controls)
{
if ( C is TextBox )
{
if ( String.IsNullOrEmpty((C as TextBox).Text))
{
// Do things this way
}
}
}
Something like this would work on the client side using jquery:
$('input').filter(function(){return this.value=="";}).css("CSSclass");
edit: I just saw the C# tag, I'll leave this here for posterity though.
What you're doing might be simple enough to do easily with some custom javascript code, though I would say that in general the built in validator controls are both easier to use and more robust than a simple validation routine that someone might write. In addition to client-side validation, the validator controls also perform server-side validation to ensure that the data submitted is truly valid, in case someone has javascript disabled in their browser.
If validator controls are included on the page, then they will include some javascript functions that you can invoke, such as Page_ClientValidate(). I believe this will return a boolean telling you whether validation passed and will trigger the visual indicators that identify what the errors are. You can execute Page_ClientValidate('') to trigger only a group of validator controls; actually I think you must do that in order to trigger validation on any validator controls that have a value in their ValidationGroup property, as I don't think Page_ClientValidate() will trigger their validation logic.
There is a CustomValidator control that you can point to your own client-side function if you want, in case you do have some special validation logic that you can only implement through a custom javascript function. This is nice to use, because then your custom javascript function will be executed by the built-in validator framework along with any other built-in validator controls that you might choose to include on the page.
Side note regarding client-side validation: I suggest that you avoid doing the following to trigger validation:
onclick="return myValidationFunction();"
because if there is any other javascript code being injected into the onclick event, your return statement will prevent it from executing. So instead, I suggest doing this:
onclick="if(!myValidationFunction()) return false;"
That's bitten me enough times that I thought I'd just throw that out there. This problem is particularly noticeable if you have an ASP.Net button on which you've set the UseSubmitBehavior property to false, as it will cause the button to render as an HTML "button" control instead of a "submit" control, and as a result, executing a return statement, either true or false, will prevent the button from triggering a postback.