16

I'm trying the pattern attribute for the first time, and I can't get it to work (my browser does support it, though).

Right now I have:

input type="text" pattern="[a-zA-Z0-9]{6}" name="formName"

The first problem is that is doesn't notify me if it's blank; the second problem is that if I do type in something, it won't accept it. I want it to accept alphanumeric characters and be exactly 6 characters is length. I tried it with forward slashes and a few other variations.

5
  • 2
    Please provide matching and non-matching input examples. Commented May 14, 2013 at 12:08
  • You can use: required="required" [Regex info][1] [1]: stackoverflow.com/a/16453203/1063823 Commented May 14, 2013 at 12:10
  • For what currently exists, nothing works except null (that I've discovered). What should work: FRA4H6, FRATHJ, 006311. Commented May 14, 2013 at 12:11
  • @Duikboot: I have posted an answer (essentially saying what you also pointed out). The reason is for people landing on this page in the future to be able to spot the right answer without having to dig through the comments. I would gladly delete my answer if you would post one yourself. Commented May 14, 2013 at 12:55
  • @Addison Burns: If one of the answers helped you solve your problem, please mark it as "accepted" so users facing a similar problem in the future will be able to spot it easily. Commented May 17, 2013 at 18:34

5 Answers 5

13

As Duikboot already pointed out, the right way to do it is:

<input type="text" name="formField" pattern="[a-zA-Z0-9]{6}" required>

The required attribute causes the validation to fail, when the field is empty.
The pattern attribute defines the regex to test against, when the field is not empty.
(Your initial pattern seems to work fine.)

More info can be found here.
This is simple enough so as not to require a demo, but nonetheless you can find one here.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey this does not work for me... <input type="text" name="card_number" pattern="[0-9]{16,16}" id="crdNo" value="<?php echo $_POST['card_number']; ?>" required/>
@Barkermn01 so you want to require a value with between 16 and 16 digits? Why not [0-9]{16}? You might choose a type more niched than type="text".
it was an example of using minLength to maxLength support in RegEx, since the example was a credit card, also any type that Is not know by the browser will default to text so you should make sure any input locking works on a type text
2

Works for me here : http://jsfiddle.net/barbuslex/nR6yg/

<form>
    <input type="text" pattern="[a-zA-Z0-9]{6}" name="formName" />
    <input type="submit" value="OK" />
</form>

I use Google Chrome

3 Comments

This doesn't address OP's first problem "The first problem is that is doesn't notify me if it's blank".
Using the latest version of Chrome, this simply does NOT work. Has this been deprecated?
it does not prevent entering the wrong data but it prevent from submitting the form.
1

You simply need to add the required attribute to your tag, which will notify the user if they attempt to send the form with that very field blank.

<input type="text" pattern="[a-zA-z0-9]{6}" name="formName" required>

Comments

-1

Try this code its working perfectly

<html>
<body>    
<form action="demo_form.asp">
  Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code">
  <input type="submit">
</form>    
</body>
</html>

Enter invalid country code and click submit button. Then You can get a message (title="Three letter country code")

Comments

-3

Inputs need to be inside "form" with "onSubmit" prop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.