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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts

    How to stop a Javascript

    Hi Guys,

    Quick question, is there a way to stop a JavaScript from validating after a certain point of the code. I've been "googling" like crazy and cant find a solution.

    I had to add a Checkbox validation last second and there was existing validation and I'm not good enough to alter all of it to reflect the checkbox part so what I did was I checked to see if at least one of the checkboxes was selected, if yes, do nothing and finish the rest of the validation. If no, prompt them with an error.

    The problem is after it prompts them for an error, it continues to run the rest of the validation and at the end it allows the end user to print. I cannot allow the end user to print until everything is filled in. Here is my code:

    Code:
    function validateFields() {
    	var fieldsToVerify = [
                    ["PrincipalInvestigator"], 
    		["ContactEmailAddress"],
    		["RFAward"],
    		["Sponsor"],
    		["AwardTitle"], 
    		["BudgetStartDate"], 
    		["BudgetEndDate"], 
    		["SectionII"],
    		["ProjectRole"],
    		["EmployeeName"], 
    		["DatesofAppointment"],
    		["EffortonProject"],
    		["Date"],
    		["PrintName"],
    		["Date2"],
    		["PrintName2"],
    		
    	];
    	
    
    		var emptyFields = [];
    	var notFullFields = [];
    	for (var i=0; i<fieldsToVerify.length; i++) {
    		var f = this.getField(fieldsToVerify[i][0]);
    		if (f==null) {
    			console.println(fieldsToVerify[i][0] + " not found.");
    			continue;
    		}
    		if (f.valueAsString=="") {
    			emptyFields.push(f.name);
    		} else if (fieldsToVerify[i].length>1 && f.valueAsString.length<fieldsToVerify[i][1]) {
    			notFullFields.push([f.name,fieldsToVerify[i][1]]);
    		}
    	}
    	
    	for (var i in nonRequiredFieldsToVerify) {
    		var f = this.getField(nonRequiredFieldsToVerify[i]);
    		if (f==null) {
    			console.println(nonRequiredFieldsToVerify[i] + " not found.");
    			continue;
    		}
    		if (f.valueAsString!="" && f.valueAsString.length<f.charLimit) {
    			notFullFields.push([f.name,f.charLimit]);
    		}
    	}
    
    
    if 
    (this.getField("CheckBox1").value=="Yes" || this.getField("CheckBox2").value=="Yes" || this.getField("CheckBox3").value=="Yes" || this.getField("CheckBox4").value=="Yes" || this.getField("CheckBox5").value=="Yes" || this.getField("CheckBox6").value=="Yes" || this.getField("CheckBox7").value=="Yes")
    
    {}
    
    else
    
    app.alert("Select a Checkbox in Section I before printing")
    
    	if (emptyFields.length==0 && notFullFields.length==0) {
    		this.print();
    	} else {
    		var msg = "";
    		if (emptyFields.length>0) {
    			msg += "The following fields must be filled-in:\n";
    			for (var i in emptyFields)
    				msg += emptyFields[i]+"\n";
    		}
    
    		if (notFullFields.length>0) {
    			if (msg!="") msg+="\n\n";
    			msg += "The following fields are not filled-in completely:\n";
    			for (var i in notFullFields)
    				msg += notFullFields[i][0] + " (required length: " + notFullFields[i][1] + ")\n";
    		}
    		app.alert(msg,"");
    	}
    }
    Last edited by hardhitter06; 03-22-2012 at 09:02 PM.

  • #2
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Id like to stop it after the part in the red until they select a checkbk and click the print button again...is this possible?

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    you can explicitly return from the function at that (or any otherplace).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    If you want it to stop a form from being submitted then you need to return false from iside your function and also return the function to the onsubmit handler.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Jun 2009
    Posts
    95
    Thanks
    15
    Thanked 0 Times in 0 Posts
    So I added the "return" after my error message for not selecting the checkbox and it prevents the rest of the script from running (perfect!)

    The problem is when I do select one of the checkboxes and hit print, the button does nothing.

    I think this was what felgall was hinting at but I'm not sure what the onsubmit handler is??


    Or I could be completely wrong but is there a way to get my script to validate from the beginning once I have made the change and hit the print button again?
    Last edited by hardhitter06; 03-26-2012 at 03:35 PM.


  •  

    Posting Permissions

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