If a user adds more then say 22 characters in the textarea, I want
to alert them and not let the page get submitted. Here is the problem, the alert comes up but the page still gets submitted.
Here is all my code.
Thanks,
S
<script language="javascript">
var strNIINList;
function LTrim(str)
/***
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim
RETVAL: An LTrimmed string!
***/
{
var whitespace = new String(" ,;|\t\n\r"
;
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...
var j=0, i = s.length;
// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;
// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}
return s;
}
function RTrim(str)
/***
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim
RETVAL: An RTrimmed string!
***/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" ,;|\t\n\r"
;
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...
var i = s.length - 1; // Get length of string
// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;
// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}
return s;
}
function trim(str)
/***
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim
RETVAL: A Trimmed string!
***/
{
return RTrim(LTrim(str));
}
function IsDelimiter(TheString){
//alert(escape(TheString));
//if (TheString == "," || TheString == ";" || TheString == " " || TheString == "|"
{
if (escape(TheString) == "%2C" || escape(TheString) == "%3B" || TheString == " " || escape(TheString) == "%0D" || escape(TheString) == "%0A" || escape(TheString) == "%7C"
{
return (true);
}
else{
return (false);
}
}
function ValidateNIIN(TheNIIN){
if(TheNIIN.length != 9 && TheNIIN.length != 0){
alert("NIIN " + TheNIIN + " is " + TheNIIN.length + " characters long. It needs to be 9 characters to be valid"
;
}
else{
strNIINList = strNIINList + TheNIIN + ",";
}
}
function ValidateTextArea(TheText){
if (TheText.length > 22){
alert("Only 100 NIINs can be processed at one time. " + TheText.length);
return(false);
}
else{
return(true);
}
}
function CheckNIINs(TheForm){
//todo:
// check each NIIN for length = 9
// valid delimiters are space, comma, semicolon,pipeline
// could be NIINs entered with no delimeter
//
//loop through the string until we come across a valid delimiter
var strString;
var intStartPos;
var intLastDelimiterPos;
strNIINList = ""
strString = trim(TheForm.BatchNiin.value)
//alert("String Length: " + strString.length);
///alert(escape(strString));
intStartPos = 0;
//alert(strString.substring(6,8));
for(x = 0;x <= strString.length - 1;x++){
//alert(x);
if(x == (strString.length - 1)){
ValidateTextArea(strString)
ValidateNIIN(strString.substring(intStartPos,x+1));
}
if(IsDelimiter(strString.substring(x,x+1))){
//alert("Current Position: " + x);
//alert("Delimiter found at position: " + x);
//alert("StartPos: " + intStartPos);
//alert("EndPos: " + x);
ValidateNIIN(strString.substring(intStartPos,x));
intLastDelimiterPos = x;
intStartPos = x + 1;
}
}
//alert(strNIINList);
var arrayofstrings;
var newarray;
newarray = new Array();
arrayofstrings = strNIINList.split(","
;
//alert(arrayofstrings.length)
//alert(arrayofstrings[1]);
y = 0;
for(x=0;x< arrayofstrings.length;x++){
//alert("here"
;
//alert((arrayofstrings[x]));
if(arrayofstrings[x] != ""
{
//alert("here2"
;
newarray[y] = " " + arrayofstrings[x];
y++;
}
}
//alert(newarray);
TheForm.txtniinhidden1.value = newarray
return (true);
}
</script>
<form method="POST" action="OSMIS_SetVar.Asp?Screen=CheckNiin.asp?Action=BATCHPARTS&RType=WS" language="JavaScript" onsubmit="return CheckNIINs(this)" id="form1" name="form1">
<input type="hidden" name="System_Level" value="INDIVIDUAL">
<input type="hidden" id="txtniinhidden1" name="txtniinhidden1">
<div align="left"><table border="0" width="100%">
<tr>
<td></td>
</tr>
<div align="center"><center><p class="redfont">Specify up to 100 <a onMouseover="showtip(this,event,'<%PrintHelp "NSNNIIN" %>')" onMouseout="hidetip()" onclick="java script:window.open('OSMIS_Desc1.asp?Action=PopUp& amp;Anchor=NSNNIIN',null,'status,scrollbars,width=
800,height=200')" style="color:Blue"><u>NIINs</u></a>:
<td align="center">
<textarea rows="20" cols="20" id="BatchNiin" name="BatchNiin"></textarea><br>
<input type="submit" value="Submit" id="submit1" name="submit1">
</td>
</tr>
<td align="center" class="redfonttwelve">
</td>
</table>
</div>
</form>
to alert them and not let the page get submitted. Here is the problem, the alert comes up but the page still gets submitted.
Here is all my code.
Thanks,
S
<script language="javascript">
var strNIINList;
function LTrim(str)
/***
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim
RETVAL: An LTrimmed string!
***/
{
var whitespace = new String(" ,;|\t\n\r"
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...
var j=0, i = s.length;
// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;
// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}
return s;
}
function RTrim(str)
/***
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim
RETVAL: An RTrimmed string!
***/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" ,;|\t\n\r"
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...
var i = s.length - 1; // Get length of string
// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;
// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}
return s;
}
function trim(str)
/***
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim
RETVAL: A Trimmed string!
***/
{
return RTrim(LTrim(str));
}
function IsDelimiter(TheString){
//alert(escape(TheString));
//if (TheString == "," || TheString == ";" || TheString == " " || TheString == "|"
if (escape(TheString) == "%2C" || escape(TheString) == "%3B" || TheString == " " || escape(TheString) == "%0D" || escape(TheString) == "%0A" || escape(TheString) == "%7C"
return (true);
}
else{
return (false);
}
}
function ValidateNIIN(TheNIIN){
if(TheNIIN.length != 9 && TheNIIN.length != 0){
alert("NIIN " + TheNIIN + " is " + TheNIIN.length + " characters long. It needs to be 9 characters to be valid"
}
else{
strNIINList = strNIINList + TheNIIN + ",";
}
}
function ValidateTextArea(TheText){
if (TheText.length > 22){
alert("Only 100 NIINs can be processed at one time. " + TheText.length);
return(false);
}
else{
return(true);
}
}
function CheckNIINs(TheForm){
//todo:
// check each NIIN for length = 9
// valid delimiters are space, comma, semicolon,pipeline
// could be NIINs entered with no delimeter
//
//loop through the string until we come across a valid delimiter
var strString;
var intStartPos;
var intLastDelimiterPos;
strNIINList = ""
strString = trim(TheForm.BatchNiin.value)
//alert("String Length: " + strString.length);
///alert(escape(strString));
intStartPos = 0;
//alert(strString.substring(6,8));
for(x = 0;x <= strString.length - 1;x++){
//alert(x);
if(x == (strString.length - 1)){
ValidateTextArea(strString)
ValidateNIIN(strString.substring(intStartPos,x+1));
}
if(IsDelimiter(strString.substring(x,x+1))){
//alert("Current Position: " + x);
//alert("Delimiter found at position: " + x);
//alert("StartPos: " + intStartPos);
//alert("EndPos: " + x);
ValidateNIIN(strString.substring(intStartPos,x));
intLastDelimiterPos = x;
intStartPos = x + 1;
}
}
//alert(strNIINList);
var arrayofstrings;
var newarray;
newarray = new Array();
arrayofstrings = strNIINList.split(","
//alert(arrayofstrings.length)
//alert(arrayofstrings[1]);
y = 0;
for(x=0;x< arrayofstrings.length;x++){
//alert("here"
//alert((arrayofstrings[x]));
if(arrayofstrings[x] != ""
//alert("here2"
newarray[y] = " " + arrayofstrings[x];
y++;
}
}
//alert(newarray);
TheForm.txtniinhidden1.value = newarray
return (true);
}
</script>
<form method="POST" action="OSMIS_SetVar.Asp?Screen=CheckNiin.asp?Action=BATCHPARTS&RType=WS" language="JavaScript" onsubmit="return CheckNIINs(this)" id="form1" name="form1">
<input type="hidden" name="System_Level" value="INDIVIDUAL">
<input type="hidden" id="txtniinhidden1" name="txtniinhidden1">
<div align="left"><table border="0" width="100%">
<tr>
<td></td>
</tr>
<div align="center"><center><p class="redfont">Specify up to 100 <a onMouseover="showtip(this,event,'<%PrintHelp "NSNNIIN" %>')" onMouseout="hidetip()" onclick="java script:window.open('OSMIS_Desc1.asp?Action=PopUp& amp;Anchor=NSNNIIN',null,'status,scrollbars,width=
800,height=200')" style="color:Blue"><u>NIINs</u></a>:
<td align="center">
<textarea rows="20" cols="20" id="BatchNiin" name="BatchNiin"></textarea><br>
<input type="submit" value="Submit" id="submit1" name="submit1">
</td>
</tr>
<td align="center" class="redfonttwelve">
</td>
</table>
</div>
</form>