Arpita
Arpita

Reputation: 1398

Restrict input string with the help of html pattern

I want to add disallow user to submit form if the user input contains 'xyz.co.in' in the input fields but this should be done with html5 pattern, I do not want to do it with jquery or javacript. Can anyone please help me with what should be its respective pattern, I mean what should go in pattern field in <input type='text' pattern='<some pattern goes here>'/>

So that user can insert anything in the input field except xyz.co.in.

UPDATE

Here is what I have so far:

<!doctype html>
<html>
    <head>
        <title>disallow a pattern</title>
    </head>
    <body>
        <form action="" >
            <input type="text" pattern="^(?!.*\bxyz\.co\.in\b)" />
            <input type="submit" name="Go1" value="Go"/>
        </form>
    </body>
</html>

Upvotes: 1

Views: 1922

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

Disallowing some string to be inside a larger string can be done with a negative look-ahead anchored at the beginning.

Here is an example that disallows xyz.co.in in an input string:

^(?!.*xyz\.co\.in).*

See demo

If you want to allow mmmxyz.co.inmmmm in the input string, add word boundaries \b around the word.

Note that pattern attribute value is anchored by default, so ^ might not be necessary, and .* is necessary to ensure the entire string is matched.

UPDATE

To disallow input of xyz.co.in and allow any other, use

^(?!xyz\.co\.in$).*

Upvotes: 3

Related Questions