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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count textarea and do not sumbit page based on count

Status
Not open for further replies.

mommy

Programmer
Aug 12, 2002
3
US
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=&quot;javascript&quot;>

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(&quot; ,;|\t\n\r&quot;);

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
// &quot;trim&quot; here in Whitespace
var whitespace = new String(&quot; ,;|\t\n\r&quot;);

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 == &quot;,&quot; || TheString == &quot;;&quot; || TheString == &quot; &quot; || TheString == &quot;|&quot;){
if (escape(TheString) == &quot;%2C&quot; || escape(TheString) == &quot;%3B&quot; || TheString == &quot; &quot; || escape(TheString) == &quot;%0D&quot; || escape(TheString) == &quot;%0A&quot; || escape(TheString) == &quot;%7C&quot;) {
return (true);
}
else{
return (false);
}
}


function ValidateNIIN(TheNIIN){
if(TheNIIN.length != 9 && TheNIIN.length != 0){
alert(&quot;NIIN &quot; + TheNIIN + &quot; is &quot; + TheNIIN.length + &quot; characters long. It needs to be 9 characters to be valid&quot;);
}
else{
strNIINList = strNIINList + TheNIIN + &quot;,&quot;;
}
}


function ValidateTextArea(TheText){
if (TheText.length > 22){
alert(&quot;Only 100 NIINs can be processed at one time. &quot; + 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 = &quot;&quot;

strString = trim(TheForm.BatchNiin.value)
//alert(&quot;String Length: &quot; + 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(&quot;Current Position: &quot; + x);
//alert(&quot;Delimiter found at position: &quot; + x);
//alert(&quot;StartPos: &quot; + intStartPos);
//alert(&quot;EndPos: &quot; + x);
ValidateNIIN(strString.substring(intStartPos,x));
intLastDelimiterPos = x;
intStartPos = x + 1;
}
}

//alert(strNIINList);
var arrayofstrings;
var newarray;

newarray = new Array();

arrayofstrings = strNIINList.split(&quot;,&quot;);
//alert(arrayofstrings.length)
//alert(arrayofstrings[1]);
y = 0;
for(x=0;x< arrayofstrings.length;x++){
//alert(&quot;here&quot;);
//alert((arrayofstrings[x]));
if(arrayofstrings[x] != &quot;&quot;){
//alert(&quot;here2&quot;);
newarray[y] = &quot; &quot; + arrayofstrings[x];
y++;
}
}

//alert(newarray);

TheForm.txtniinhidden1.value = newarray
return (true);
}

</script>






<form method=&quot;POST&quot; action=&quot;OSMIS_SetVar.Asp?Screen=CheckNiin.asp?Action=BATCHPARTS&RType=WS&quot; language=&quot;JavaScript&quot; onsubmit=&quot;return CheckNIINs(this)&quot; id=&quot;form1&quot; name=&quot;form1&quot;>

<input type=&quot;hidden&quot; name=&quot;System_Level&quot; value=&quot;INDIVIDUAL&quot;>
<input type=&quot;hidden&quot; id=&quot;txtniinhidden1&quot; name=&quot;txtniinhidden1&quot;>
<div align=&quot;left&quot;><table border=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td></td>
</tr>
<div align=&quot;center&quot;><center><p class=&quot;redfont&quot;>Specify up to 100 <a onMouseover=&quot;showtip(this,event,'<%PrintHelp &quot;NSNNIIN&quot; %>')&quot; onMouseout=&quot;hidetip()&quot; onclick=&quot;java script:window.open('OSMIS_Desc1.asp?Action=PopUp& amp;Anchor=NSNNIIN',null,'status,scrollbars,width=
800,height=200')&quot; style=&quot;color:Blue&quot;><u>NIINs</u></a>:

<td align=&quot;center&quot;>
<textarea rows=&quot;20&quot; cols=&quot;20&quot; id=&quot;BatchNiin&quot; name=&quot;BatchNiin&quot;></textarea><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot; id=&quot;submit1&quot; name=&quot;submit1&quot;>

</td>
</tr>
<td align=&quot;center&quot; class=&quot;redfonttwelve&quot;>
</td>
</table>
</div>
</form>

 
You have to add
[tt]return false;[/tt]
to your CheckNIINs() function after the alert.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top