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.
Page 3 of 9 FirstFirst 12345 ... LastLast
Results 31 to 45 of 124
  1. #31
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Johnny87auxsm View Post
    Phillip sure am mate 100%, im using the developer tools in chrome, displays no ERRORS ? Can you check and see if it also displays no errors on ur side?
    It works for me - assuming you have a checkbox with the id of "agreed". (Not "agree").

    Code:
    <input type = "checkbox" id = "agreed">
    
    <script type = "text/javascript">
    
    // If AGREE isn't ticked no alert, if it's left blank an alert dialog will appear	
    
    if (document.getElementById('agreed').checked == false) {
    alert ("You didn't agree to the terms");
    //return false;  //  return can only appear in a function
    } 
    else {
    alert("OK")
    //return true;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  2. #32
    rff
    rff is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Johnny87auxsm View Post
    Phillip sure am mate 100%, im using the developer tools in chrome, displays no ERRORS ? Can you check and see if it also displays no errors on ur side?
    I don't think you know how to use the developer console... It will not log logic errors... Click on sources, find the javascript, click on the line(s) to set a debug marker, and watch the local variables. If you need further explanation, google it first.

  3. #33
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    It works for me - assuming you have a checkbox with the id of "agreed". (Not "agree").

    Code:
    <input type = "checkbox" id = "agreed">
    
    <script type = "text/javascript">
    
    // If AGREE isn't ticked no alert, if it's left blank an alert dialog will appear	
    
    if (document.getElementById('agreed').checked == false) {
    alert ("You didn't agree to the terms");
    //return false;  //  return can only appear in a function
    } 
    else {
    alert("OK")
    //return true;
    }
    
    </script>
    Phillip that code works yes but when u put it in my javscript file it works up until female/male alert, this code ^ doesn't display an alert

  4. #34
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I don't see anything about male/female. The code tests whether a checkbox about agreeing terms has been checked. But whatever the outcome of the test the script ends there - with either return false or return true. No further tests or comparisons will be processed.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  5. #35
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't see anything about male/female. The code tests whether a checkbox about agreeing terms has been checked. But whatever the outcome of the test the script ends there - with either return false or return true. No further tests or comparisons will be processed.
    Here is the copied from my JS file, see the first validation with comments " //Gender /Female it works but the second validation under it which you helped with isn't working?
    Code:
    		
    		//GENDER / Female WORKING 
     	 	var booValid = true;
     	 	var strErrorMsg = "";
      
     	 	if ((!formtag.gender[0].checked) && (!formtag.gender[1].checked))
      
     	 	{
      
      		strErrorMsg += "Must select Female or Male\n";
      		booValid=false;
      	
      		}
     
     	 	if(!booValid)
     	 	{
      
      		alert(strErrorMsg);
      
      		return booValid;
      		}
       
       
    			
    			
    			
    			if (document.getElementById('agreed').checked == false) {
    alert ("You didn't agree to the terms");
    //return false;  //  return can only appear in a function
    } 
    else {
    alert("OK")
    //return true;
    }
    			
    }

  6. #36
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here i attached it as it is so you can see lol, makes it alot easier to understand what im talking about..
    Attached Files Attached Files

  7. #37
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Although it might work in most browsers (might!!) you should never rely on using the id attribute as a global variable. This is just "bad habit".

    So instead of
    Code:
    formtag.gender[0].checked
    you should do
    Code:
    var theForm = document.getElementById('formtag');
    var theGender = theForm.getElementsByName('gender');
    if(!theGender[0].checked && !theGender[1].checked)

  8. #38
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    ... even more so because most browsers won't do that with a STRICT doctype

  9. #39
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Still not working, can u check my code which is attached above and let me know why ??

  10. #40
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Oh, for crying out loud!

    OF COURSE the validation STOPs with the gender check!

    *** LOOK AT YOUR CODE ***

    After I have formatted it into a semblance of readability:
    Code:
    var booValid = true;
    var strErrorMsg = "";
    if ((!formtag.gender[0].checked) && (!formtag.gender[1].checked))
    {
        strErrorMsg += "Must select Female or Male\n";
        booValid=false;
    }
    if(!booValid)
    {
        alert(strErrorMsg);
        return booValid;
    }
    IF THE GENDER IS NOT CHECKED, then you RETURN FALSE right then and there!

    But your whole validation is nearly worthless!

    If I bring up the page and *ONLY* check male or female and *ONLY* check "agree to terms"--NOTHING ELSE--and then hit the SUBMIT FORM button, look what happens.

    Yes, you give messages about the missing name, phone number, etc., BUT YOU STILL SUBMIT THE FORM!!!

    What is the point in giving those messages if you are going to submit the form, anyway?

    IF THERE IS AN ERROR, of any kind, in the validation, then the validation function *MUST* return false or the form *WILL* be submitted.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  11. #41
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    And you WERE TOLD that this crap is INVALID:
    Code:
        	var fNameString = document.getElementById('fname');
    How can you get that utterly wrong and then do it right in the very next code block, where you do
    Code:
       	 	var fNametest = document.getElementById("fname").value;
    And then, as DevNull *TOLD* you, you repeat the same error, over again:
    Code:
        var lNameString = document.getElementById('gname');
        var carerString = document.getElementById('parent');
    I shouldn't do this, but let me show the dirt simple way to do a MUCH better, and much easier, job of validating the first name:
    Code:
    var valid = true;
    
    var fnameRE = new RegExp(/^[-a-zA-Z]{3,35}$/);
    if( ! fnameRE.test( document.getElementById("fname").value ) )
    {
        alert("You must give your first name, from 3 to 35 characters, letters and hyphen only");
        valid = false; // and do this on each validation failure
    }
    NEVER set valid to true except at the top of the validation, as shown.
    And then, at the very end of the validation, simply do return valid;

    This is what you get for trying to just copy bits and pieces of other people's code instead of making an effort to understand what is going on.

    And if you *HAD* used the Chrome debugger, and *HAD* put a breakpoint on the line after
    Code:
        var fNameString = document.getElementById('fname');
    and *HAD* simply inspected that variable, you would have seen that it was total nonsense.

    So learning to use the error console is a start, but you would have saved yourself hours of frustration if you had also learned how to use breakpoints and code single step in the debugger.
    Last edited by Old Pedant; 05-04-2013 at 01:39 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  12. #42
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The code your saying is incorrect is the exact code im using from my university prescribed textbook, so im following as it shows ??

    How would i stop the form from submitting if there is validation errors? By the way i followed what u said and used this and now all validations are working..

    Code:
    	var booValid = true;
     	 	var strErrorMsg = "";
      
     	 	if ((!formtag.gender[0].checked) && (!formtag.gender[1].checked))
      
     	 	{
      
      		strErrorMsg += "Must select Female or Male\n";
      		booValid=false;
      	
      		}
     
     	 	if(!booValid)
     	 	{
      
      		alert(strErrorMsg);
     		 }

  13. #43
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually it doesn't submit the form at all, i added my asp link in the action of the form and it doesn't submit now whch is good.. Code looks like this

    Code:
    //GENDER / Female WORKING 
     	 	var booValid = true;
     	 	var strErrorMsg = "";
      
     	 	if ((!formtag.gender[0].checked) && (!formtag.gender[1].checked))
      
     	 	{
      
      		strErrorMsg += "Must select Female or Male\n";
      		booValid=false;
      	
      		}
     
     	 	if(!booValid)
     	 	{
      
      		alert(strErrorMsg);
      
      		}
       
       
    			
    			
    			
    			
    		// If AGREE isn't ticked no alert, if it's left blank an alert dialog will appear	
    		if (document.getElementById('agreed').checked == false) 
     		{
    
      		alert ("You didn't agree to the terms");
    
      		return false;
    
    		 } 
     		else
      		{
    
      		return true;
    
    		 }

  14. #44
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ALso this bit of code below, It makes sense but could i do something like this for dates between 2001 and 2004 ?

    Code:
    var valid = true;
    
    var fnameRE = new RegExp(/^[-a-zA-Z]{3,35}$/);
    if( ! fnameRE.test( document.getElementById("fname").value && document.getElementByID ("dob".value) > 2004 && document.getElementByID ("dob".value) <2001  ) )
    {
        alert("wrong date and years are wrong ");
        valid = false; // and do this on each validation failure
    }
    Obviously the code is wrong just trying to get my logic Correct first to see how i would go about using a Regex with a date of birth entered between 2000 and 2001 -.-

  15. #45
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Don't use a RegEx. Except to check that the format entered was good.

    I use something like this:
    Code:
    // generic check-for-valid-date function
    function checkDate( test ) 
    {
        var date = test.replace( /[\.\-\s]/g, "/" ); // change all dashes, periods spaces to /
        var dateRE = /^\d\d?\/\d\d?\/\d\d\d\d$/;
        var parts = date.split("/");
        if ( ! dateRE.test(date) || parts.length != 3)
        {
            alert("Dates must be entered in the format mm/dd/yyyy");
            return null;
        }
        var month = Number(parts[0]) - 1;
        var check = new Date( parts[2], month, parts[1] );
        if ( check.getMonth() != month )
        {
            alert("That is not a valid date");
            return null;
        }
        return check;
    }
    
    // code to specifically check the form's DOB value
    var date = checkDate( myform.dob.value );
    if ( date == null )
    {
        valid = false;
    } else {
        var year = date.getYear();
        if ( year < 2000 || year > 2004 )
        {
            alert("DOB must be between 2000 and 2004");
            valid = false;
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 3 of 9 FirstFirst 12345 ... LastLast

Posting Permissions

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