Tanker
12-09-2010, 04: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 "Quote "/><br />
Output: <input type="text" id="output" style="width: 500px;" /><br />
<input type="button" value="Go" onclick="testan(document.getElementById('input').value);" />
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 "Quote "/><br />
Output: <input type="text" id="output" style="width: 500px;" /><br />
<input type="button" value="Go" onclick="testan(document.getElementById('input').value);" />