 <p>You can try this regex:</p> <pre><code>^(?=.*?[a-zA-Z])(?=.*?[0-9])[\w@#$%^?~-]{5,30}$ </code></pre> <h3>Live Demo &amp; Examples: <a href="http://www.rubular.com/r/EXHHCoq0WC" rel="nofollow">http://www.rubular.com/r/EXHHCoq0WC</a></h3> <p><strong>Explanation:</strong></p> <ul> <li><code>^</code> is line start</li> <li><code>(?=.*?[a-zA-Z])</code> is a positive lookahead that will make sure there is atleast one alphabet</li> <li><code>(?=.*?[0-9])</code> is a positive lookahead that will make sure there is atleast one digit</li> <li><code>[\w@#$%^?~-]{5,30}</code> is using character class for 5 to 30 characters specified inside square brackets</li> <li><code>$</code> is line end</li> </ul> <h3>Lookaround Reference: <a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow">www.regular-expressions.info/lookaround.html</a></h3>