// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // Creation Date: 26 March 2003 // // // // // // 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. // // // string $string The name string to test. // string $regularExpression The regular expression. // // // int : True if the name is valid, false otherwise. // // // // // // 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. // // // global proc int isValidString(string $string, string $regularExpression) { int $result = false; if ("" != $string && "" != $regularExpression) { if ($string == match($regularExpression, $string)) { $result = true; } } return $result; }