Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Detecting Non English Characters

    My credit card processing service will only accept English characters. I need to detect entries in the form that are using non English characters.

    Is there a way in Javascript to create a function to force a string to UTF-8? My idea is to do something like

    function forceUTF8(X){
    Y = ???? X;
    return Y;
    }

    if ( stringA != forceUTF8(stringA) alert ("You must use English Characters");

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    How on earth would UTF-8 compliance help with non-english characters?

    UTF-8 includes tons and tons of non-english characters ...

    Try this instead
    Code:
    if(!/^[a-zA-Z]*$/.test(stringA)) alert('Attention: Don't use non-english characters!');

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Way cool, thanks Dev. If I need any additional characters I assume I can add them to the set.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,294
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Code:
    var text=myTextInput.value;
    var hits=0;
    
    if(hits=text.match(/([\x80-\uffff])/g) ){ 
      alert(  "Non-English characters detected: " + hits ); //complain to user
      myTextInput.value= text.replace( /([\x80-\uffff])/g, "" );//remove offending chars
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hmm, I experimented with both those but could not get them to work.
    FWIW this is what I ended up with.
    It is a little messy but the text strings are quite short so it should not be too much processing load.


    Code:
    function ForeignText(txt){ 
    	for(i=0;i<txt.length;i++){ 
    		if(txt.charCodeAt(i)>2048) return 1;
    	}
    	return 0;
    }

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    How in the world did you decide on 2048???

    ANY character code outside the range 0 to 127 will *NOT* be an ASCII character, which is what I would assume you meant by non-English.

    Here, a counter example to your silly choice of 2048:
    Code:
    <script>
    for ( c = 128; c < 2048; ++c )
    {
        document.write( String.fromCharCode(c) );
    }
    </script>
    Do *THOSE* look like "English" characters to you???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    LMAO, I just picked a number that would be higher than English characters and lower than the Japanese characters my customers were using on the website. I found that all the naughty characters were over 10,000 and wasn't sure how high English went so I picked a number that would be somewhere in the middle.

    Its working great, got my first order for $275 from Japan within an hour after finishing the site. Hope that is a good sign.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •