dgreenie
07-19-2012, 10:13 AM
Hi i am noob to javascript, so please help me. thanks a lot.
For my case,
my javascript is this,
function doAdd() {
var catstr = document.getElementById("cat").value;
var valstr = document.getElementById("val").value;
var numpattern = new RegExp("^[0-9]+\.?[0-9]*$");
if(!numpattern.test(valstr)) {
alert("Non numeric data in values field");
return;
}
var catpattern = new RegExp("^[A-Za-z0-9 ]+$");
if(!catpattern.test(catstr)) {
alert("Invalid data in category name field");
return;
}
var cats = document.getElementById("catlist");
var nums = document.getElementById("numlist");
if(numadded>0) {
cats.value = cats.value + "\n";
nums.value = nums.value + "\n";
}
numadded++;
cats.value = cats.value + catstr;
nums.value = nums.value + valstr;
}
And for my html page,
<html>
<head>
<title>PieForm</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="checksubmit.js"></script>
</head>
<body>
<h1>Pie Graph Data Entry</h1>
<form method="post" action="Piegraph.php" onsubmit="return checkData()">
<table border="1">
<tr>
<th colspan="2">
Data item
</th>
</tr>
<tr>
<th>Category</th>
<th><input type="text" name="Category" Id="cat"/></th>
</tr>
<tr>
<th>Value</th>
<th><input type="text" name="Value" Id="val"/></th>
</tr>
<tr>
<th colspan="2">
<input type="button" name="doAdd" value="Add Item" onclick="return doAdd()" >
</th>
</tr>
<tr>
<th colspan="2">Date for submission</th>
</tr>
<tr>
<th>Category</th>
<th>Value</th>
</tr>
<tr>
<th><textarea rows="4" readonly="readonly" name="catlist" Id="catlist"></textarea></th>
<th><textarea rows="4" readonly="readonly" name="numlist" Id="numlist"></textarea></th>
</tr>
<tr>
<th>Title for plot</th>
<th><input type="text" name="title" Id="title">
</tr>
<tr>
<th colspan="2"><input type="submit" name="plotgraph" value="Plot graph">
</th>
</tr>
<tr>
<th colspan="2"><input type="reset" name="reset" value="Reset" >
</th>
</tr>
</form>
</body>
</html>
Just wondering how come when i type stuff into my category and value and i add item, the results doesn't appear in my read only text area?? haha
Thanks!
For my case,
my javascript is this,
function doAdd() {
var catstr = document.getElementById("cat").value;
var valstr = document.getElementById("val").value;
var numpattern = new RegExp("^[0-9]+\.?[0-9]*$");
if(!numpattern.test(valstr)) {
alert("Non numeric data in values field");
return;
}
var catpattern = new RegExp("^[A-Za-z0-9 ]+$");
if(!catpattern.test(catstr)) {
alert("Invalid data in category name field");
return;
}
var cats = document.getElementById("catlist");
var nums = document.getElementById("numlist");
if(numadded>0) {
cats.value = cats.value + "\n";
nums.value = nums.value + "\n";
}
numadded++;
cats.value = cats.value + catstr;
nums.value = nums.value + valstr;
}
And for my html page,
<html>
<head>
<title>PieForm</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="checksubmit.js"></script>
</head>
<body>
<h1>Pie Graph Data Entry</h1>
<form method="post" action="Piegraph.php" onsubmit="return checkData()">
<table border="1">
<tr>
<th colspan="2">
Data item
</th>
</tr>
<tr>
<th>Category</th>
<th><input type="text" name="Category" Id="cat"/></th>
</tr>
<tr>
<th>Value</th>
<th><input type="text" name="Value" Id="val"/></th>
</tr>
<tr>
<th colspan="2">
<input type="button" name="doAdd" value="Add Item" onclick="return doAdd()" >
</th>
</tr>
<tr>
<th colspan="2">Date for submission</th>
</tr>
<tr>
<th>Category</th>
<th>Value</th>
</tr>
<tr>
<th><textarea rows="4" readonly="readonly" name="catlist" Id="catlist"></textarea></th>
<th><textarea rows="4" readonly="readonly" name="numlist" Id="numlist"></textarea></th>
</tr>
<tr>
<th>Title for plot</th>
<th><input type="text" name="title" Id="title">
</tr>
<tr>
<th colspan="2"><input type="submit" name="plotgraph" value="Plot graph">
</th>
</tr>
<tr>
<th colspan="2"><input type="reset" name="reset" value="Reset" >
</th>
</tr>
</form>
</body>
</html>
Just wondering how come when i type stuff into my category and value and i add item, the results doesn't appear in my read only text area?? haha
Thanks!