tags:

views:

224

answers:

3

in my webapplication i have user registration page, in that one textbox contain House No: user has to enter houseno in that textbox. i that user can enter all numarics or all alphabets or all alphanumarics but not all special characters how can i restrict the user not to enter all special characters like user can enter

123455 or abcdef or 12abcdef

but not

&&&&&& or

or

@@@@ or * user should not enter the specail charecters give me some solution thank u

A: 

I think that the regular expression you need is (\w|\d)+

EDIT: \w already includes digits, so: \w+

Konamiman
This would match true if there was one non-special character. It wouldn't limit the input to only non-special characters.
ck
Hi Mr. Konamiman Cam it restriing to not use all special characters but for exmple i enter like this "H-No : 41-259#" it gives error but i want to enter one specail charecters like this, i think u understand my problem other wise i will explain again thank u for response
Surya sasidhar
Surya sasidhar
+1  A: 

simply ^\w+$

  • ^ indicates the start of the string the regex pattern is applied to

  • \w matches letters, digits and underscores.

  • + matches one or more occurrences (in this case applied to letters, digits and underscores)

  • $ indicates the end of the string the regex pattern is applied to

If you didn't want underscores matched then

^[a-zA-Z0-9]+$

If the field was optional, then you may want to use * in place of + as this matches zero or more occurrences.

Russ Cam
Hi Mr. Russ Cam it restriing to not use all special characters but for exmple i enter like this "H-No : 41-259#" it gives error but i want to enter one specail charecters like this, i think u understand my problem other wise i will explain again thank u for response
Surya sasidhar
Surya sasidhar
i try that ^\w+$ not working i mean when i enter in the textbox like this "41-259#" it is giving error, but i want to allow that one thank you for response
Surya sasidhar
which special characters **do you want to allow**?
Russ Cam
Given your comment, change the regular expression to `^[\w-#]+$`
Russ Cam
`[\w-#]`? Surely you mean `[\w#]`.
Roger Pate
@Roger - Either `[\w#]` or `^[\w-#]+$` will work for input `41-259#`, but they are not finding the same matches. Try it out in Firebug for JavaScript of using NRegex (http://www.nregex.com/nregex/default.aspx) for C# regular expressions. `[\w#]` will not match the hyphen
Russ Cam
@Russ: I'm surprised it works, `[\w-#]` looks like an invalid character range.
Roger Pate
A: 

The standard way to achieve this in asp.net is to use a RegularExpressionValidator control. A simple example is as follows:

    <asp:Label runat="server" ID="HouseNoLabel" Text="House No." AssociatedControlID="HouseNoTextBox" />
    <asp:TextBox runat="server" ID="HouseNoTextBox" />
    <asp:RegularExpressionValidator runat="server" ID="HouseNoValidator" ControlToValidate="HouseNoTextBox"
    ErrorMessage="* only alphanumeric characters are allowed" ValidationExpression="[\w]+" />

If you would like to stop a user from entering invalid values altogether, take a look at the MaskedEdit control from the Ajax Toolkit http://www.asp.net/ajax/AjaxControlToolkit/Samples/

rob
Mr.rob thank you for response but it is not working see it is allowing only alpha numarics but not special character like "41-259#" then it is giving error i said not all special character but combination of alphanumarics and specail characters allowed
Surya sasidhar