问题
How to validate using Javascript an input tag of type text in an html form that the content of it will be one of the following:-
- Arabic or English Characters ONLY -OR-
- Arabic and English Characters ONLY -OR-
- Arabic characters only -OR-
- English Characters only -OR-
- Number only -OR-
- Arabic Characters and Number only -OR-
- English Characters and Number only -OR-
- Arabic or Number ONLY -OR-
- English Characters or Number ONLY -OR-
- Arabic or English Characters or Number ONLY -OR-
- Phone Number -OR-
- Email -OR-
- English string that starts with specific character like 'A' -OR-
- English string that ends with specific character like 'A' -OR-
- English string that contains specific character like 'A' -OR-
- English string that does not contain specific character like 'A'
Thanks in advance ..
回答1:
First, I need a definition of "English", "Arabic" and "Numbers" characters.
- The definition of the English character boundary should at least include a-z, A-Z. Non-word characters (comma, dot, parentheses) should also be included, but since you didn't specify the purpose of your validation, I will limit these to a-z, A-Z.
English RegExp:[a-zA-Z]
- I'm not skilled in the Arabian language, so I grab include all characters as definied at this source.
RegExp:/[\u0600-\u06ff\ufb50-\ufdff\ufe70-\ufeff]/
- The numbers are defined as
\d
(in JavaScript:0-9
).
RegExp:\d
(equals[0-9]
in JavaScript) - The beginning and the end of a string are matched by a
^
and$
, using Regular expressions.
Brought together:
- Arabic and/or English Characters ONLY -OR-
/^[a-zA-Z\u0600-\u06ff\ufb50-\ufdff\ufe70-\ufeff]+$
- Arabic characters only -OR-
/^[\u0600-\u06ff\ufb50-\ufdff\ufe70-\ufeff]+$/
- English Characters only -OR-
/^[a-zA-Z]+$/
- Number only -OR-
/^\d+$/
- Arabic Characters and/or Number only -OR-
/^[\du0600-\u06ff\ufb50-\ufdff\ufe70-\ufeff]+$
- English Characters and/or Number only -OR-
/^[\dA-Za-z]+$/
- Arabic and/or English Characters and/or Number ONLY -OR-
/^[a-zA-Z\d\u0600-\u06ff\ufb50-\ufdff\ufe70-\ufeff]+$/
- Phone Number -OR- Phone numbers are too locale-dependent, construct your own RE
- Email -OR-
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
source 2 (very basic mail RegExp) - English string that starts with specific character like 'A' -OR-
/^A/
- English string that ends with specific character like 'A' -OR-
/A$/
- English string that contains specific character like 'A' -OR-
/A/
- English string that does not contain specific character like 'A'
^[^A]+$
Of course, the English character set should include more characters than a-zA-Z
if you're validating sentences. I recommend to use [\x20-\x7e]
instead of [a-zA-Z]
, so commonly used punctuation characters are also available.
References / See also
- MDN: Regular expressions - A guide to use Regular Expressions in JavaScript
- Regulsr Expressions.info - Summary of character boundaries
- UTF8-chartable.de - Browser through all characters
- Unicode.org/charts - An official reference which documents all boundaries in a deeper detail
来源:https://stackoverflow.com/questions/7620010/how-to-validate-using-javascript-an-input-tag-of-type-text-in-an-html-form-that