Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Halt Code Execution

    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.

    Code:
    <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);" />

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

    Code:
    <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)
    Last edited by Philip M; 12-09-2010 at 05:57 PM.

  • Users who have thanked Philip M for this post:

    Tanker (12-09-2010)

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.
    Code:
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •