Solved

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

Posted on 2013-06-06
11
402 Views
Last Modified: 2013-06-06
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
Comment
Question by:us1975mc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 39226535
Look on page 2
The javascript works
http://forums.asp.net/t/1130796.aspx/2/10
0
 

Author Comment

by:us1975mc
ID: 39226548
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
 
LVL 22

Expert Comment

by:plusone3055
ID: 39226573
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:us1975mc
ID: 39226580
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
 
LVL 22

Expert Comment

by:plusone3055
ID: 39226603
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
 

Author Comment

by:us1975mc
ID: 39226646
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
 
LVL 22

Expert Comment

by:plusone3055
ID: 39226663
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
 
LVL 22

Accepted Solution

by:
plusone3055 earned 500 total points
ID: 39226665
forgot the _ at the end of the other side
0
 

Author Closing Comment

by:us1975mc
ID: 39226699
That was the ticket!

Thanks!
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 39226713
Ooooh raaaah !
0
 

Author Comment

by:us1975mc
ID: 39226719
Semper Fi, Mac!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question