User501
User501

Reputation: 347

Have to make validate the Month with hypen using JS

Need to validate the month by using the hypen as like JAN-FEB, FEB-MAR... likewise. Here i have followed the code,

function myFunction() {
  var patt=/^((["JAN"]+)|(["FEB"]+))$/
  text = document.getElementById("p01").innerHTML;
  document.getElementById("demo").innerHTML = patt.test(text);
}
<p id="p01">JAN-FEB</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Once I validate this I'm getting the false, but I want this to be true. It have to validate by only the month name of starting 3 letter with capital letter as like JAN-FEB, JAN-DEC, FEB-JUN,.... likewise for all types of month.

Upvotes: 0

Views: 47

Answers (4)

Hassan Imam
Hassan Imam

Reputation: 22564

You can use regex to get the month name from the p tag and check for each month to be present in months array using array#every.

var myFunction = function() {
  var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
  text = document.getElementById("p01").innerHTML;
  var match = text.match(/(\w+){3}-(\w+){3}/g);
  document.getElementById("demo").innerHTML = match ? match[0].split('-').every(month => months.includes(month)) : false;
}
<p id="p01">JAN-FEB</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Upvotes: 1

Saksham
Saksham

Reputation: 9380

Please see the snippet below with relevant comments. For this to be fool-proof, you need to perform several checks as below

function validateMonthRange() {
    var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];  //Array of months
    var monthVal = $('selector').split('-'); //get the 2 months value
    if(monthVal.length == 2) {  //if there are 2 values only
        var startIndex = months.indexOf(monthVal[0].toUpperCase());
        var endIndex = months.indexOf(monthVal[1].toUpperCase());
        if(startIndex >= 0 && endIndex >= 0 && startIndex <= endIndex) {
            //valid if both values are in array and start month is less than or equal to the end month
        }
    }

Upvotes: 0

Baha
Baha

Reputation: 408

Sorry I can't add a comment, but your question isn't very clear, if you want to validate that you have 3 capital letter followed by a hyphen then capital letter characters, then you could use something like:

/[A-Z]+\-[A-Z]+/

or for only three letters you duplicate like: /[A-Z][A-Z][A-Z]-[A-Z][A-Z][A-Z]/

This of course won't check the spilling of the months. please add a comment if I didn't understand correctly!

Upvotes: 0

Charlie
Charlie

Reputation: 23828

Here is one way you can do it. The idea is to take the month string, separate it by the hyphen and test it for a set of predefined moth abbreviations.

function myFunction() {

   var monthsAbvs = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']


   var monthPattern = document.getElementById("p01").innerHTML;

   var months = monthPattern.split('-');     //This creates an array with two separated strings

   if (months.length > 2)                     //Only two month abbreviations are expected
      return false;

   if (monthsAbvs.indexOf(months[0]) < 0 || (monthsAbvs.indexOf(months[1]) < 0)   //Both montsh should be in our list
      return false;


   return true;   //Everything is good by here 


}

Upvotes: 1

Related Questions