1

I have a simple text field which I need to validate with a regex.
The text field is supposed to contain inline-styles in the "proper" format xxx: xxx;

Ex: font-size: 15px; background: #33333; -webkit-transition: background-color 500ms ease-out 1s;

Any regex gurus out there help me out here. I'll eventually find the solution on my own but that may take awhile.

Languages would be javascript for client side validation then moving onto php for the server side validation.

Thanks a bunch guys... I came up with this -- /([a-z-]+: [0-9a-z#(). -]+;$)/g --. I would answer myself but I'm unsure if its the best solution.

Gonna accept the most detailed answer below from @nu11p01n73R as I stole the $ sign from his explanation that made the above work right.

Now is there an issue with what I came up with? Know I'm not technically supposed to ask but I think its easier to read for me to read, anyways. I love SO.

3
  • And what have you tried so far? Commented Nov 11, 2014 at 17:41
  • Is that example a singl input or different inputs. Give an example which should not be validatede Commented Nov 11, 2014 at 17:42
  • [a-z-]+: matches the first part of it... its a single input it should match only the format above as many times as needed to allow the user to add multple inline styles. I only care about ensuring the format of the styles... not if they are an actual style... thats on them. Commented Nov 11, 2014 at 17:46

2 Answers 2

1

You can use a regex of the form

^([^:]+:[^;]+;)+$

see for example http://regex101.com/r/cE6yS6/3

  • ^ anchors the regex at the begining of the string. ensures that nothing presceds the matched string

  • [^:]+: matches anything other than : followed by a :

  • [^;]+; matches anything other than ; followed by ;

  • + quantifier, repeats the occurence of property:value;

  • $ anchors the regex at the end of the string. Ensures nothing follows the matches string.

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

Comments

0

try this better solution :

^([a-zA-Z\-\s]+:[a-zA-Z0-9\-\s#%\(\)\+']*;)+$

it handles exception case:

ex1=font-size: 15px; background: #33:333; //not match
ex2=font-size: 15px; background: #33333;  //match

for html:

<input type="text"  pattern="^([a-zA-Z\-\s]+:[a-zA-Z0-9\-\s#%\(\)\+']*;)+$" style="width: 1000px;"/>

4 Comments

I'll give that a try real quick n get back to ya
Your solution works but the solution I posted above in my questions also works just as well but I think it is easier to read. Thanks though man. I know how it is trying to answer questions on here. I still am going to keep the answer previously accepted as it defines what the 'moving parts' are doing and I don't want to keep checking back to continually update acceptance. Cheers and thanks again.
previous ans sometimes no works, which is said in exception case: in my ans.
this solution only works in forms at submit

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.