1
  • I tried to allow input either Chinese or Alphabetic Character.

     var name="TEXT" //here input either alphabetic or Chinese Characters 请输入
     let reqChinesePos=/^[\u3000\u3400-\u4DBF\u4E00-\u9FFF]\d{8}$/;
     let reqEnglish=/^[A-Za-z]\d{40}$/
    
     console.log(reqEnglish.test(name)); //Here true but here also Chinese characters also match.
     console.log(reqChinesePos.test(name)); //Here true but here also alphabetic characters also match.
    

Expected result :

console.log(reqEnglish.test(name));//here only allow alphabetic characters not allow chinese.
console.log(reqChinesePos.test(name));//here only allow Chinese characters not allow English.
sameer
  • 447
  • 3
  • 12
  • 20
  • See how to correctly match all Chinese chars [here](https://stackoverflow.com/a/54179646/3832970). Your `[\u3000\u3400-\u4DBF\u4E00-\u9FFF]` only includes few ranges. – Wiktor Stribiżew Mar 25 '19 at 09:18

1 Answers1

1

Your character class range for Chinese characters seems correct, at least from simple local testing here. But, I see another problem with both patterns, in that the \d should probably be part of the character class, if you want to also allow Roman numerals in both cases. After making this change, then give a width or range of widths for the input. Assuming you wanted a width of 8 both, you could try:

var reqChinesePos = /^[\u3000\u3400-\u4DBF\u4E00-\u9FFF\d]{8}$/;
var reqEnglish = /^[A-Za-z\d]{8}$/

var name1 = "大猫大猫大猫大猫";
var name2 = "JONATHAN";
var name3 = "BIGCAT大猫";

console.log(name1);
console.log(reqEnglish.test(name1));
console.log(reqChinesePos.test(name1));

console.log(name2);
console.log(reqEnglish.test(name2));
console.log(reqChinesePos.test(name2));

console.log(name3);
console.log(reqEnglish.test(name3));
console.log(reqChinesePos.test(name3));
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360