Javascript uses regular expressions to control input values in the input box.

Source: Internet
Author: User

1. Only numbers are allowed.

<input name="username" type="text" onkeyup="value=this.value.replace(/\D+/g,'')">

2. Only English letters, numbers, and underscores (_) are allowed)

<input name="username" type="text" style="ime-mode:disabled"><input name="username" type="text" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">

3. Only English letters, numbers, and = @ # are allowed @#

<input name="username" type="text" onkeyup="value=value.replace(/[^\w=@#]|_/ig,'')">

4. Only uppercase letters and numbers are allowed.

<Input name = "name" type = "text" value = "only uppercase letters and numbers are input." style = "color: gray" onfocus = "this. value = ''; this. style. color = 'black' "onkeyup =" this. value = this. value. replace (/[^ A-Z0-9]/gi, ''); this. value = this. value. toLocaleUpperCase ();

5. Only Chinese characters are allowed.

<input name="username" type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">

[Filter text input]

TextField. restrict = "this is input content"; field. restrict = "^ this is forbidden content ";

The restrict attribute supports regular expressions:

Field. restrict = "a-zA-z"; // only the field size is allowed. restrict = "a-zA-z"; // only letters and spaces are allowed. restrict = "0-9"; // only the number field is allowed. restrict = "^ abcdefg"; // field is allowed except for the lowercase letter abcdefg. restrict = "^ a-z"; // all lower-case letters are not allowed, but other contents are allowed, including field, an upper-case letter. restrict = "0-9 ^ 5"; // only numbers are allowed, except for 5

Make the restrict character contain special letters (such as-and ^ ):

Field. restrict = "0-9 \-"; // allows numbers and broken Number Fields. restrict = "0-9 \ ^"; // allow numbers and ^ field. restrict = "0-9 \\\\"; // allow numbers and backslash

You can also use the Unicode escape sequence to specify the allowed content. For example:

field.restrict = "^\u001A";

Note: ActionScript is case-sensitive. If the restrict attribute is set to abc, uppercase letters (A, B, and C) are allowed, B and c), and vice versa. the restrict attribute only affects the content that users can enter. The script can put any text into text fields.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.