1
  • Trying to avoid space start and end of the string only allowed in between the string
  • Minimum 2 character required
  • special character and number validation

So I added a code like but not working for minimum two character validation

<form action="/action_page.php">
    Full Name: 
   <input type="text" name="full_name"
          pattern="^[a-zA-Z]+ (\s+[a-zA-Z]+)*$" 
          title="Enter your Full name( First name, Last name)">
   <input type="submit">
</form>
7
  • No special character and number validation - what do you mean by that? are those allowed or not? Commented Aug 23, 2018 at 9:09
  • Not allowed it is fixed but need to have minimum two character validation Commented Aug 23, 2018 at 9:10
  • Three letter country code? Why 2 char limit then? Commented Aug 23, 2018 at 9:17
  • Yeah... what do you mean by space only allowed between the string when it has to be 2 characters and space can't be first and last? That logically means that spaces are not allowed at all. And this code even without character limit doesnt accept "G B" ... maybe some examples? Commented Aug 23, 2018 at 9:20
  • And what country code has spaces inside to begin with? Commented Aug 23, 2018 at 9:21

3 Answers 3

1

Add minlength="2" (see minlength support table) to restrict the minimum length to 2 chars and remove the space between + and ( as spaces are meaningful in regex patterns.

<form action="/action_page.php">
    Full Name: 
   <input type="text" name="full_name" minlength="2"
          pattern="^[a-zA-Z]+(\s+[a-zA-Z]+)*$" 
          title="Enter your Full name( First name, Last name)">
   <input type="submit">
</form>

Note that you may also remove ^ and $ in the pattern and use

pattern="[a-zA-Z]+(\s+[a-zA-Z]+)*"

because HTML5 engine will put the pattern in between ^(?: and )$, thus ensuring an entire string match.

If browsers that do not support minlength should be supported, use a lookahead check at the start:

pattern="(?=.{2})[a-zA-Z]+(\s+[a-zA-Z]+)*"

The (?=.{2}) will require 2 chars immediately after start of the string is asserted (mind pattern="(?=.{2})[a-zA-Z]+(\s+[a-zA-Z]+)*" will be translated into /^(?:(?=.{2})[a-zA-Z]+(\s+[a-zA-Z]+)*)$/ regex, with or without u modifier depending on the browser).

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

1 Comment

@SaurabhGujarani Glad it worked for you. Please consider also upvoting if my answer proved helpful to you.
0

Found answer for pattern replace to this

^(?=.{2,}$)([a-zA-Z]+(\s+[a-zA-Z]+)*)$

3 Comments

ok, good job... next time please be more specific when asking
Sure!!! I just added example my bad to explain it what actually we want next time I will take care Thanks for your time :)
@SaurabhGujarani You do not need to resort to lookaheads with HTML5. See my answer.
0

Try with this pattern

<form action="/action_page.php"> Country code: <input type="text" name="country_code" pattern="[a-zA-Z]{2}" title="Two letter country code"> <input type="submit"> </form>

By default, it will not allow space at the start and end of the string and it has validation of minimum 2 characters and the third case will also work.

1 Comment

its not working with the space between character e.g Jai P showing error

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.