I need pattern for a quarter of a year, for example 1/2015 -> first quarter of the 2015. 4/2015 -> fourth quarter of the year. I need to validate given value for being a quarter. How I can achieve that in javascript or php ? I guess that in PHP preg_match function is good way to go but I have difficulties with creating correct pattern...
It depends on the range of years that you'd like to support.
Assuming anything between quarter 1/1000 and 4/2999 should be accepted then the regular expression you want to use is
/^[1234]\/[12]\d{3}$/
For example this JavaScript function will test for valid quarters
function is_quarter(text) {
return text.match(/^[1234]\/[12]\d{3}$/) != null;
}
Mm. Not 100% sure I understand, but how about this?
function is_quarter($string) {
$parts = explode('/', $string);
if(count($parts) == 2) {
$quarter = (int) $parts[0];
return $quarter >= 1 && $quarter <= 4;
}
return false;
}
function isQuarter(string) {
var parts = string.split('/');
if(parts.length == 2) {
var quarter = parseInt(parts[0]);
return quarter >= 1 && quarter <= 4;
}
return false;
}
Try this:
function Check(){
var v = document.getElementById("Quarter").value;
alert(/^[1-4]\/\d{4}$/.test(v));
}
<input type="text" id="Quarter" />
<input type="button" value="Check" onclick="Check();" />
</div>