简体   繁体   中英

korean and alphabet regex returning false

I have been having trouble validating a form input for my website. Im looking the accept both english characters and korean characters for the name field. I had no trouble with inputting korean characters but at soon as I added the korean regex, the function has been returning false while without it returns true.

var recommend_coffee_your_name_field = $('#coffee_recommend_your_name_field');

recommend_coffee_your_name_field.keyup(function(e) {
  var $th = $(this);
  $th.val( $th.val().replace(/[^a-zA-Z0-9\u{3130}-\u{318F}\u{AC00}-\u{D7AF}]/g, function(str) { 
    alert('You typed " ' + str + ' ".\n\nPlease use only letters and numbers.'); return ''; 
  } ) );

  if(e.keyCode !== 8 || e.keyCode !== 9 ) {             
    var recommend_coffee_your_name_field_length = recommend_coffee_your_name_field.val().length;
    //console.log("Your Name Field Length: " + your_name_field_length);

    if( recommend_coffee_your_name_field_length > 0 ){
      //recommend_coffee_your_name_field.removeClass("error").addClass("success");
      recommend_coffee_shop_your_name_value = true;
    }
    else{
      //recommend_coffee_your_name_field.removeClass("success").addClass("error");
      recommend_coffee_shop_your_name_value = false;
    }
    if (!recommend_coffee_shop_your_name_value) {
      alert("The name field is missing");
      e.preventDefault();
    } 
});

In your case, it is enough to remove the curly braces from the Unicode escape sequences:

/[^a-zA-Z0-9\u3130-\u318F\uAC00-\uD7AF]/g

Note that \\u{3130} is only possible in ECMAScript 6 compatible browsers, please refer to ECMAScript 6 Compatibility Table .

The JavaScript String Literals Guide calls \\uXXXX Unicode escape sequences :

The Unicode escape sequences require at least four characters following \\u\u003c/code> .
Example : = ©

And \\u{XXXXX} are called Unicode code point escapes :

New in ECMAScript 6. With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF . With simple Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same.

Example : \\u{2F804} // The same with simple Unicode escapes: \?\?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM