Using Regular expression( Regex ), We can check whether the given string has minimum no. of characters or whether it is inside specified range etc. We use ^ and $ to count length of string from the beginning to the end. For example, following regex will match a string which has 5 characters:

So, if we want check whether the given string length is in the range between 5 to 7, we use:

Its okay, but how we use this in real time, huh? There will be slight changes between the scripts in implementing regex. Below i have given you examples in javascript and php.

Check minimum and maximum length in Javascript:

<script type="text/javascript">
var str='';
var regexRange=/^[a-zA-Z]{5,7}$/;

alert('Inside given range');
alert('Exceeds given range');


Check minimum and maximum length in PHP:

echo 'Inside given range';
echo 'Exceeds given range';


Comments (0)
Leave a Comment

loader Posting your comment...