script (MEL) |
Only available in
MEL |
isValidString
|
In categories: Language, Strings |
Go to: Synopsis. Return value. Related.
MEL examples.
int isValidString(string $string, string
$regularExpression)
Return true if the string is valid according to the regular
expression argument. Use "[a-zA-Z][a-zA-Z0-9_]*" as the regular
expression for strings that must begin with a letter and is
followed by letters, digits, or underscores (no spaces allowed).
Examples of valid strings are: "Name", "New_Name", and "Name1". Use
"[a-zA-Z][a-zA-Z0-9_ ]*" as the regular expression for strings that
must begin with a letter and is followed by letters, digits,
underscores, or spaces. Examples of valid strings are: "Name", "New
Name", and "Name 1". Use
"[0-9][0-9][0-9]\\\.[0-9][0-9][0-9]\\\.[0-9][0-9][0-9][0-9]" as the
regular expression for strings that are 10 digit phone numbers.
Periods must follow the area code and exchange, ie. the exact
format must be ddd.ddd.dddd where 'd' is a single digit. The
regular expression is implicitly bound to the start and end of the
string, so a pattern of "abcd" will give the same result as a
pattern of "^abcd$" although the latter may execute slightly faster
in some cases. Note: isValidString is not reliable on strings
containing multibyte data.
None
isValidObjectName, isValidUiName
Variable Name |
Variable Type |
Description |
$string |
string |
The name string to test. |
$regularExpression |
string |
The regular expression. |
// Regular expression does not allow spaces.
//
isValidString("Name1", "[a-zA-Z][a-zA-Z0-9_]*"); // Will succeed.
// Regular expression does not allow spaces.
//
isValidString("My Name", "[a-zA-Z][a-zA-Z0-9_]*"); // Will fail.
// Regular expression does allow spaces.
//
isValidString("My Name", "[a-zA-Z][a-zA-Z0-9_ ]*"); // Will succeed.
// Regular expression must be a 10 digit phone number.
//
isValidString("204.555.9663",
"[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9][0-9]"); // Will succeed.
isValidString("(204)555-9663",
"[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9][0-9]"); // Will fail.
isValidString("204-555-9663",
"[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9](\\\.)[0-9][0-9][0-9][0-9]"); // Will fail.