HTML5 patteren attribute

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 378
  • Loc: Canada

Post 3+ Months Ago

Hi All

I have a form that can give information on a domain name (not subdomains) and on IP addresses.

When the user enters the domain name or IP address into the appropriate form input box, I validate it to see if it is of the correct format.

For domain name I use:
pattern="^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$"

For IP Address I use (IPV4):
pattern="((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$"

They both work properly if the user enters the information directly.

However, if the user copies the domain or IP address and happens to copy a white space before and after the information, and pastes it into the input box, the form displays a format error.
If I remove the beginning and ending space it works fine.

Can someone please show me how to ignore the beginning and ending white spaces so that the format becomes valid.
Is there a particular place it must be inserted.
I am assuming that if only the beginning white space or ending white space is copied, it will be removed. I do not want the pattern to look for two white spaces if there is only one, if I have explained myself correctly.

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 378
  • Loc: Canada

Post 3+ Months Ago

By luck I managed to find the syntax:
\s* to account for any white spaces.

For domain name I used:
pattern="^\s*([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}\s*$"


For IP Address I used (IPV4):
pattern="((^\s*|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}\s*$"

For the IP address I had to put it inside the anchors ^ and $ or else it did not work.

Now it does not matter how many white spaces are at the beginning or at the end the format becomes valid

Hope this helps someone

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 24 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.