Tigger Tigger - 7 months ago 21
Python Question

How to make sure a textarea has string values separated with commas

I have tried googling this info for hours and am not sure if I am just not searching correctly. Everything I have seen says their textarea has information separated by commas and they want to do something else. I am trying to figure out how to actually make sure users enter string values separated by commas (so either while entering a message occurs if they enter a ; etc or upon submit a validation occurs). I am wanting the values separated by commas because upon submit, I have the strings entered into an array using split with commas as the deliminator. I am using html, javascript, and python. Please don't ask me to use other languages or to not use cgi as I don't have the capability of adding anything to my class computer. Thank you for any help!

HTML code:

<html>
<head>
<script type='text/javascript'>
function track()
{
var xhttp = new XMLHttpRequest();
var tnum=document.getElementById('tnum').value; //get value of textarea
if (trimAll(document.getElementById('tnum').value) === '')
{
alert('Please enter a tracking number!');
}

xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
document.getElementById('trackinfo').innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", 'http://localhost:8000/cgi-bin/track_project_fxn.py', true);
xhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhttp.send('tnum='+tnum);
}
function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}
</script>
</head>
<body bgcolor="33cccc">
<br>
<h3>Tracking</h3>

<form id="trackform">
<textarea form="trackform" id="tnum" rows="10" cols="45" style="resize:none" placeholder="Enter up to 25 tracking numbers, one per line, and separated by commas">
</textarea>
<br><Br>
<input type="button" value="Submit" onclick="track()">&nbsp;&nbsp;
<input type="reset" value="Clear">

</form>
<div id="trackinfo">
</body>
</html>


Python code:

import cgi, cgitb, re
cgitb.enable

form=cgi.FieldStorage()
tnum=form.getvalue('tnum')
unum=tnum.upper()
snum=re.sub('[\s+]', '',unum)
tlist=snum.split(',')


print("Content-type:text/html\r\n\r")
print()

for tnumber in tlist:
<logic>

Max Max
Answer

You could use a regular expression to check the value of the textarea. If I understand your problem the user should only be able to enter numbers and commas. Moreover the textarea should not start with a comma and end with a comma. You could check the value of the textarea after the user submits the form:

var xhttp = new XMLHttpRequest();
var tnum=document.getElementById('tnum').value; //get value of textarea
if (trimAll(document.getElementById('tnum').value) === '')
{
 alert('Please enter a tracking number!');
 return false; // I think you forgot this
}
if (wrongFormat(tnum)){
 alert("Only numbers and commas");
 return false;
}

[..]

function wrongFormat(s){
if (s.match(/[^0-9,]/)) return true; // with letters /[^0-9A-Za-z,]/
if (!s.charAt(0).match(/[0-9]/) || !s.charAt(s.length-1).match(/[0-9]/)) return true;
if (s.match(/[,][,]/)) return true; // check if there are 2 commas in a row
}