RegexValidator for mobile number validation in Python
RegexValidator:-
A RegexValidator searches the provided value for a given regular expression with re.search(). By default, raises a ValidationError with message and code if a match is not found. Its behavior can be inverted by setting inverse_match to True, in which case the ValidationError is raised when a match is found.
class RegexValidator(regex=None
, message=None
, code=None
, inverse_match=None
, flags=0)
Parameters:-
regex à If not None, overrides regex. It can be a regular expression string or pre-compiled regular expression.
message à If not None, overrides message.
code à If not None, overrides code.
inverse_match à If not None, overrides inverse_match.
flags à If not None, overrides flags. In that case, regex must be a regular expression string, or TypeError is raised.
regex à The regular expression pattern to search for within the provided value, using re.search(). This may be a string or a pre-compiled regular expression created with re.compile(). Defaults to the empty string, which will be found in every possible value.
message à The error message used by ValidationError if validation fails. Defaults to "Enter a valid value".
code à The error code used by ValidationError if validation fails. Defaults to "invalid".
inverse_match à The match mode for regex. Defaults to False.
flags à The regex flags used when compiling the regular expression string regex. If regex is a pre-compiled regular expression, and flags is overridden, TypeError is raised. Defaults to 0.
Example:
mobile_number = RegexValidator(regex="^[0-9]{10,15}$", message="Entered Mobile Number is not in right format..!")
Comments
Post a Comment