disable characters from being entered in textbox - asp.net

I am building a form and i want to prevent certain characters from being typed in a textbox?
I want to disable the " and ' characters in some of my textboxes

You can use a RegularExpressionValidator along with a regex such as [^"'] to allow all characters except " and '.
(please note, that regex is untested at the moment...)
ASP.NET Validators have both client and server APIs for validation.

The safest way is to check the form values on the server-side to see if the input is valid (doesn't include quote characters in your case) and respond with an error if it is invalid.
On the client side, I've had good luck using jQuery Validation.
Remember that arbitrary form payloads can be constructed outside of a browser, so you always have to check data validity on the server, even if client-side validation is in place.

Using an AJAX ASP FilterTextBox Control would be the way I'd go.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="TARGETCONTROLIDHERE"
FilterType="Custom, Numbers, LowerCaseLetters, UpperCaseLetters, Symbols"
InvalidChars="&" />

You can simply remove those characters from the string before you perform your database operation.

Related

ASP.NET Regular Expression Validator

I need a Regular Expression that can validate an exact 3 character(alpha only) code but also a blank field to set as the validation expression of a ASP.NET RegEx validator control.
I am currently using ^[a-zA-Z]{3}$
and this works out well to match the code but of course doesn't match a blank.
I have been looking at using something like this:
^(?:|)[a-zA-Z]{3}$
If your intention is to allow blank fields, then use the original pattern of ^[a-zA-Z]{3}$ since the RegularExpressionValidator doesn't validate blank fields. It will allow them.
However, if you want to prevent blank entries then you'll need to add a RequiredFieldValidator to validate the same control, in addition to the RegularExpressionValidator.
Have you tried using (^$)|(^[a-zA-Z]{3}$)?

only two digits allowed after decimal asp.net

I have a textbox where the user must not be able to enter more than two digits after a decimal.How do I do this without using javascript?
Thanks.
You can set the MaxLength property of the textbox, but that doesn't have any notion of whether or where the decimal point is.
You could also use a CustomValidator and check the inputted number on the server via the ServerValidate event. But this will require going to the server to check the value (i.e. it will initially look like your form allows users to input invalid numbers).
You should also be able use to a RegularExpressionValidator, depending on your exact globalization requirements, which will use JavaScript on the client to provide immediate feedback:
<asp:TextBox ID="NumberTextBox" runat="server" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="NumberTextBox"
ValidationExpression="\d+(?:(?:\.|,)\d{1,2})?" />
If you want the immediate feedback to the user, you'll need to use a JavaScript based solution.
You cannot cause the textbox to stop accepting text after two decimal places without directly or indirectly using javascript. (This is sometimes called an input mask).
You can, however, allow the user to enter free-form text and validate the text upon postback on the server. You can either automatically round the number for them, or return an error message to the client.
If you really need to prevent the user from entering more than two digits after the decimal point, you'll need to use JavaScript or a server control that implements the JavaScript for you.
However, may make more sense to allow them to enter any number of digits and then catch it on validation (or just round to two digits).

Test if the user has typed date format ASP.NET (VB)

I have two textbox called BIRTH & EMAIL, and I must test in button click if the user has typed a date & email format in those two TextBox.
How can do that ?
Take a look at DateTime.TryParse for the date.
For the e-mail, unfortunately using regular expressions is probably your best bet.
Use a RegularExpressionValidator for each field, in conjunction with a RequiredFieldValidator. There are some ready made regular expressions for both that can be accessed through the properties window of the control.
The ValidationExpression is where you set the regular expression.
Why dont you use the ASP .NET Validators?
http://support.microsoft.com/kb/316662
Seems like the RegularExpressionValidator along with the RequiredFieldValidator would be perfect for this.
Note: The Regular Expression Validator already has the email option in the pre populated list.
For the date field, I would use the date picker, or some other control that let the user enter the day, month, and year separately. That way you don't have to worry about ambiguous dates, such as 01/05/2010, or even worse 01/05/10 (is that y/m/d, d/m/y, m/d/y etc.), and other such problems. For email address, I would do a simple check for the "#" symbol, and a dot, but don't get any more stringent than that. Email addresses can be pretty weird. You can also make them type the email address twice to ensure that they typed it correctly.
As you're using VB.NET, I believe IsDate(dateValue) is a simple option to validate the date.
Regular Expressions are your best bet though.
There's a fairly long discussion on SO regarding email regexes here:
Using a regular expression to validate an email address

ASP.NET email validator regex

Does anyone know what the regex used by the email validator in ASP.NET is?
Here is the regex for the Internet Email Address using the RegularExpressionValidator in .NET
\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
By the way if you put a RegularExpressionValidator on the page and go to the design view there is a ValidationExpression field that you can use to choose from a list of expressions provided by .NET. Once you choose the expression you want there is a Validation expression: textbox that holds the regex used for the validator
I don't validate email address format anymore (Ok I check to make sure there is an at sign and a period after that). The reason for this is what says the correctly formatted address is even their email? You should be sending them an email and asking them to click a link or verify a code. This is the only real way to validate an email address is valid and that a person is actually able to recieve email.
E-mail addresses are very difficult to verify correctly with a mere regex. Here is a pretty scary regex that supposedly implements RFC822, chapter 6, the specification of valid e-mail addresses.
Not really an answer, but maybe related to what you're trying to accomplish.
We can use RegularExpressionValidator to validate email address format. You need to specify the regular expression in ValidationExpression property of RegularExpressionValidator. So it will look like
<asp:RegularExpressionValidator ID="validateEmail"
runat="server" ErrorMessage="Invalid email."
ControlToValidate="txtEmail"
ValidationExpression="^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" />
Also in event handler of button or link you need to check !Page.IsValid.
Check sample code here : sample code
Also if you don't want to use RegularExpressionValidator you can write simple validate method and in that method usinf RegEx class of System.Text.RegularExpressions namespace.
Check example:
example
For regex, I first look at this web site: RegExLib.com
Apart from the client side validation with a Validator, I also recommend doing server side validation as well.
bool isValidEmail(string input)
{
try
{
var email = new System.Net.Mail.MailAddress(input);
return true;
}
catch
{
return false;
}
}

ASP.Net Validation

I want to validate the value a user enters in a text box, so that it only enters float numbers. I'm not interested in range. How can I do this, considering also culture localization information (e.g. "." or "," as separators)?
My usual method is to use a RegexValidator with a validation expression of ^(\d+(\.\d*)?)|(\d*(\.\d+))$. You could ammend this to enable "." or ",": ^(\d+([\.,]\d*)?)|(\d*([\.,]\d+))$.
If you wanted to be strictly correct, you'd enable the correct validation expression for each culture.
Also note that you still need a RequiredFieldValidator if the value is compulsary.

Resources