Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html input pattern with 2 patterns

Is it possible to have a input pattern with 2 options?

For example. I have a field where a user enters an extension number (4 Digits) eg. 7483 BUT Some people have 2 extensions eg. 7483 / 7542

Is there a way to set the input pattern so it can either be 4 characters or 11? And otherwise show a error message?

This is what I have so far:

Extension:<br><input type="text" name="extension" required
pattern= "{4}" title="Please Enter the Extension Number/s"><br />
like image 811
RedZ Avatar asked Sep 13 '26 06:09

RedZ


1 Answers

You can use jQuery Form Validator lib & write custom validator for it. For validation you should use /^\d{4}( \/ \d{4})?$/ pattern.

Here is the complete solution. Read it thorough & stick with RegExp! ;-)

<!DOCTYPE html>
<html>
<head>
    <title>Custom Validation</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form action="" method="post">
        Extension:<br />
        <input type="text" name="extension" required
               data-validation="custom" data-validation-regexp="^\d{4}( \/ \d{4})?$" title="Please Enter the Extension Number/s"><br />
    </form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
    <script>
        // Setup form validation
        $.validate();
    </script>
</body>

like image 182
BehradKhodayar Avatar answered Sep 14 '26 20:09

BehradKhodayar