...

View Full Version : Halt Code Execution



Tanker
12-09-2010, 05:50 PM
I have code that I have written to clean up the value of a text box before submitting it to a database, the problem I have is that if the value is blank, I want to throw up an alert and halt the JS from continuing, normally I would put in a return false statement and be done, but since the function is already returning a value I was wondering if there was another way to stop code execution.

So if cleanString is not blank after it processes code should continue, if it is blank it should stop everything after the alert message. I know I can code for it on the calling side but I was hoping to include it all in one function. If I return false after the alert the output box actually says false and code continues to process.



<script>
function cleanString(myString){
//Removes All Special Characters and Double Spaces and trims the text
temp = myString.replace(/[^a-zA-Z 0-9]+/g,'')
temp = temp.replace(/^\s+|\s+$/g, '');
temp = temp.replace(/\s+/g, ' ');
if(temp == ""){
alert('Input string cannot be blank, or contain special characters.');
//Halt Code execution here
} else {
return temp;
}
}

function testan(i){
document.getElementById('output').value = cleanString(i);
alert("still going");
}
</script>

Input: <input type="text" id="input" style="width: 500px;" value=" Test (Parens) [brackets] 1234 2 4 ,Comma &quot;Quote "/><br />
Output: <input type="text" id="output" style="width: 500px;" /><br />
<input type="button" value="Go" onclick="testan(document.getElementById('input').value);" />

Philip M
12-09-2010, 06:40 PM
As yu say, only one value can be passed in a return statement. But you can use the value of another global variable to determine the code to be executed in a second function.


<script type = "text/javascript">

var temp; // must be global
var OK; // must be global
function cleanString(myString){
OK = true;
document.getElementById('output').value = ""; // clear output box
//Removes All Special Characters and Double Spaces and trims the text
temp = myString;
temp = temp.replace(/^\s+|\s+$/g, '');
temp = temp.replace(/\s+/g, ' ');
if (/[^a-zA-Z 0-9]/g.test(temp) || (temp.length == 0)) {
OK = false;
alert('Input string cannot be blank, or contain special characters.');
document.getElementById('input').value = ""; // clear input box
return OK;
}
else {return temp}
}

function testan(i){
cleanString(i);
if (OK) {
setTimeout("document.getElementById('output').value = temp", 25);
}
}
</script>
</head>
<body>

Input: <input type="text" id="input" style="width: 500px;" value=" Test (Parens) [brackets] 1234 2 4 ,Comma &quot;Quote "/><br />
Output: <input type="text" id="output" style="width: 500px;" /><br />
<input type="button" value="Go" onclick="testan(document.getElementById('input').value);" />


“Two things reduce prejudice: education and laughter.” - Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

Tanker
12-09-2010, 07:39 PM
Thanks Phillip, I tried something similiar but the actual code it's being called from is a little more in depth involving a string of input fields names that are split, a case statement and building a URL to be passed via AJAX call and returning data.

Currently i'm using the following to do it, but it involves calling cleanString 2 times, but it does stop execution in it's tracks. We need an abort command. :D


if(!cleanString(document.getElementById(myFields[1]).value)){alert("Vendor Name cannot be blank, or contain special characters.");return false;};
myURL += "&venName=" + cleanString(document.getElementById(myFields[1]).value);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum