Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Special Characters in a text box - don't want them

I have a web page that collects people’s names , addresses and so on.  There is a problem when someone enters their information and uses special characters in their name or addresses.  Such as Âlice Pâté.  I tried using a RegularExpressionValidator  with the ValidationExpression="^[a-zA-Z]{1,40}" and they can still get the special characters through.  

How can I prevent this.  I am using Visual Studio 2010 with Visual Basic on the aspx pages.

Any ideas?
<asp:RegularExpressionValidator ID="regexpName" runat="server"     
                                    ErrorMessage="This expression does not validate." 
                                    ControlToValidate="TextBox1"     
                                    ValidationExpression="^[a-zA-Z]{1,40}" />

Open in new window

0
us1975mc
Asked:
us1975mc
  • 6
  • 5
1 Solution
 
plusone3055Commented:
Look on page 2
The javascript works
http://forums.asp.net/t/1130796.aspx/2/10
0
 
us1975mcAuthor Commented:
plusone3055,

I currently have this code:
<script type="text/JavaScript">
    function valid(f) {
        !(/^[A-z;0-9]*$/i).test(f.value) ? f.value = f.value.replace(/[^A-z;0-9]/ig, '') : null;
    } 
</script>

 <asp:TextBox ID="TextBox1" runat="server" Text="" Width="400px"  onkeyup="valid(this)" onblur="valid(this)"/>

Open in new window


Problem here is it don't allow spaces.  How do I get a space in there?
0
 
plusone3055Commented:
try
<script type="text/JavaScript">
    function valid(f) {
        !(/^[A-z;0-9\d\s]*$/i).test(f.value) ? f.value = f.value.replace(/[^A-z;0-9]/ig, '') : null;
    }
</script>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
us1975mcAuthor Commented:
When I leave the textbox it takes all spaces out.  So if I placed in 'Alice Pate' as soon as I leave the textbox it changes to 'AlicePate'
0
 
plusone3055Commented:
try
<script type="text/JavaScript">
    function valid(f) {
        !(/^[A-z;0-9 _]*$/i).test(f.value) ? f.value = f.value.replace(/[^A-z;0-9]/ig, '') : null;
    }
</script>


 !(/^[A-z;0-9 _]*$/i).test

NOTE there is a "_"  before the closing bracket
0
 
us1975mcAuthor Commented:
OK... Here is where it becomes strange.  It works fine if I don't give a special character.  If I enter 'Alice Pate' it gives 'Alice Pate' but is I enter 'Âlice Pàte' it gives 'AlicePate' it gives 'licePte'.  It still takes the space if a special character is added and it is fine if no special character is entered.
0
 
plusone3055Commented:
try
<script type="text/JavaScript">
    function valid(f) {
        !(/^[A-z;0-9 _]*$/i).test(f.value) ? f.value = f.value.replace(/[^A-z;0-9 _]/ig, '') : null;
    }
</script>
0
 
plusone3055Commented:
forgot the _ at the end of the other side
0
 
us1975mcAuthor Commented:
That was the ticket!

Thanks!
0
 
plusone3055Commented:
Ooooh raaaah !
0
 
us1975mcAuthor Commented:
Semper Fi, Mac!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now