บทช่วยสอนนี้สอนวิธีการใช้ JavaScript เพื่อ จำกัด การป้อนข้อมูลเป็นภาษาอังกฤษหรือ จำกัด การป้อนข้อมูลเป็นภาษาจีน
ห้ามภาษาอังกฤษ:
การคัดลอกรหัสมีดังนี้:
<อินพุต type = "text" onkeypress = "return event.keycode> = 48 && event.keycode <= 57 || (this.value.indexof ('.') <0? event.keycode == 46: false)" onpaste = "return!
ใช้การแสดงออกปกติเพื่อ จำกัด ภาษาจีนเท่านั้น:
การคัดลอกรหัสมีดังนี้:
<input type = "text" onkeyup = "value = value.replace (/[^/u4e00-/u9fa5]/g, '')"/>
1. ใช้นิพจน์ทั่วไปเพื่อ จำกัด อักขระเต็มความกว้างเท่านั้น:
การคัดลอกรหัสมีดังนี้:
<อินพุต type = "text" onkeyup = "value = value.replace (/[^/uff00-/uffff]/g, '')"/>
2. ใช้นิพจน์ทั่วไปเพื่อ จำกัด เฉพาะอินพุตตัวเลข:
การคัดลอกรหัสมีดังนี้:
<input type = "text" onkeyup = "value = value.replace (/[^/d]/g, '')" onbeforepaste = "clipboarddata.setData ('text', clipboardData.getData ('text') แทนที่ (/[^/d]/g, '))
3. ใช้นิพจน์ทั่วไปเพื่อ จำกัด เพียงตัวเลขและภาษาอังกฤษ:
การคัดลอกรหัสมีดังนี้:
<input type = "text" onkeyup = "value = value.replace (/[/w]/g, '')" onbeforepaste = "clipboardData.setData ('text', clipboardData.getData ('text') แทนที่ (/[^/d]/g, '))"