Hulk
Hulk

Reputation: 34170

JavaScript CSV validation

How to check for comma separated values in a text box and raise an alert if not found? And if there is it should be characters in it like A,B,C,D

  function validate()
   {
         //validate text box;
   }
  <input type="text" id="val" >A,B,C,D</input>
  <input type="button" id="save" onclick="validate()">  
 

Upvotes: 3

Views: 9665

Answers (3)

jatin
jatin

Reputation: 1439

You should use a csv library for validating, one of the good ones out there is http://papaparse.com/

var  val = document.getElementById('val').value;
var results = Papa.parse(csvString, config)
var errors = results["errors"] 

Upvotes: 2

ravindrakhokharia
ravindrakhokharia

Reputation: 174

I hope this will help you

function validate()
{
    val = document.getElementById('val').value;
    val = val.split(',');
    alert(val[0].length);
    for(var i=0;i<val.length;i++)
    {
        if(val[i].length != 1){
            alert("Please check value should be coma seperated at every single characters");
            return false;
        }
    }
    return true; 
}

HTML:

<input type="text" id="val" value = 'A,B,C,D' ></input>
<input type="button" id="save" onclick="return validate()">  

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284816

/^[A-Za-z](?:,[A-Za-z])*$/.test(document.getElementById("val").value)

Upvotes: 3

Related Questions