Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate comma delimited recordset in a textarea.

Status
Not open for further replies.

uncleroydee

Technical User
Nov 28, 2000
79
0
0
US
I have to let users update a database over the web. As part of the "select list" they will need to enter comma delimited record numbers using a text area. They will be permitted to "hand jam" the data or they can copy and paste comma delimited files from a spreadsheet or text editor into the textbox. I plan to size the textbox so they can paste up to 1,000 numbers at once.

I'm already trimming spaces and validating that the records are all numeric, but I also need to ensure that each record contains 13 characters between the commas, ensure that there is no comma after the last record, and fire an alert on errors. Does anyone have an idea, or script, for accomplishing that?

Thanks,
Roy
 
To verify that there are 13 characters between each character, you chould make use of the split command. This splits the entire string into an array using the comma as a separator. Then just iterate through the array using a loop and use the length property to verify that each array element is 13 characters long.

You could also use the charAt function to verify that the last character is not a comma. i.e. something like

myString = document.myForm.myField.value
if (myString.charAt(myString.Length) == ',') {
alert("Cannot end with a ,");
}

Hope this helps. Mise Le Meas,

Mighty :)
 
Hi,

I have a similar requirement wtr textarea but at the end i need to highlight the invalid enrties
that is if there are 20 comma seperated values and 5th,9th and 17th are invalid (in this case less than 13 characters) then i have to highligth those values only.
How can i select a part of the text from a function??

thanks in advance
Aravind
 
You need to validate each value as you take it and then even if it is invalid you use it's length to help you get the next value. PHP is much better for doing this kind of thing as you can make an array of values seperated by commas. As far as I know, there is no way to do this in Javascript.

function checkData(){
var dbOK = new Array(); // stores correct values
var dbBad = new Array(); // stores incorrect values
var myLength = 6; // the correct length of a value
var myData = document.myForm.myTextarea.value + ",";
var thisData = "";

// loop while you have data left
for(i=0;myData.indexOf(",")>0;i++) {
// get the first value
thisData = myData.substring(0,myData.indexOf(","));
// shorten your data
myData = myData.substring(myData.indexOf(",")+1,myData.length)
// if it's the right length, add it to the OK data
if (thisData.length == myLength) {
dbOK[dbOK.length] = thisData;
}
// otherwise add it to the errors
else {
dbBad[dbBad.length] = thisData;
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top