SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Form Submits on Javascript Return False

    Apparently I'm seeing what I expect to see instead of what's really there.

    (I've looked at the javascript SitePoint responses for the myriad of others who have same complaint and for the life of me ...)

    Here's the Javascript code (that produces the correct messages)


    Code:
    function ValidateOnSubmit()
    {
    	var elem;
    	var errs=0;
    	// execute all element validations in reverse order so focus
    	// gets set on first one in error
    	
      if(!validatePresent(document.forms.addForm.articleText, 'inf_text')) errs += 1;
      if(!validatePresent(document.forms.addForm.articleSynopsis, 'inf_synopsis')) errs += 1;
      if(!validatePresent(document.forms.addForm.articleTitle, 'inf_title')) errs += 1;
      if (errs>1) alert('There are fields which need correction before sending');
      if (errs==1) alert('There is a field which needs correction before sending');
      return (errs==0);
    }
    And here's the form
    Code:
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    ...
        <form id="addForm"
             action="/legacy/article/updatecontroller.php"
             method="post"
             onsubmit="ValidateOnSubmit()">
        <fieldset id="fieldSynopsis"> 
         <legend>Synopsis</legend>
         <div id="divSynopsis">
          <label for="articleTitle" accesskey="T">Article Title [T]</label>
          <span id="inf_title" class="msg">Required</span>
          <textarea id="articleTitle" 
                    name="articleTitle" 
                    tabindex="2" 
                    cols="83"
                    rows="1"
                    onchange="validatePresent(this, 'inf_title')"></textarea>
    ...
        <fieldset id="fieldOptions">
         <legend>Options</legend>
         <div id="divOptions">
         <input type="submit" name="optPic" id="optPic" class="clsOptions" value="Add Picture" tabindex="4" />
         <input type="submit" name="optPreview" id="optPreview" class="clsOptions" value="Preview" tabindex="5" />
         <input type="submit"
                   name="optSave"
                   id="optSave"
                   class="clsOptions"   
                   value="Add"     
                   tabindex="6"  />
          <input type="submit" name="optCancelIt" id="optCancelIt" class="clsOptions" value="Cancel" tabindex="7" />
    ...
    I get the error messages and the alert 'There are fields which need correction before sending' so I know the individual Javascript "validatePresent" is/are working.

    But I also get the message from the server validation routine after I click the "... fields need correction ..." alert box so I know it's submitting.

    Help greatly appreciated.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You need to return the result of the ValidateOnSubmit() function

    Code javascript:
    <form id="addForm"
        action="/legacy/article/updatecontroller.php"
        method="post"
        onsubmit="return ValidateOnSubmit()">
    Last edited by paul_wilkins; Jun 11, 2008 at 17:33.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You need to return the result of the ValidateonSubmit() function
    onsubmit="return ValidateOnSubmit()">
    Yep. .. of COURSE! Nice catch!

    Thank you.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI; return (errs==0); is the same as return !errs;
    mmj

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    What's a good way to pluralize text strings.

    Perhaps something like this?

    Code javascript:
    String.prototype.plural = function (num, text) {
    	return (num > 1) ? text : this;
    }
    alert('There ' + 'is a field'.plural(errs, 'are fields') +
    	' which ' + 'needs'.plural(errs, 'need') +
    	' correction before sending');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    What's a good way to pluralize text strings.

    Perhaps something like this?

    Code javascript:
    String.prototype.plural = function (num, text) {
    	return (num > 1) ? text : this;
    }
    alert('There ' + 'is a field'.plural(errs, 'are fields') +
    	' which ' + 'needs'.plural(errs, 'need') +
    	' correction before sending');
    ?
    mmj

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    When I was loking at his code from the first message, I saw the following

    Code javascript:
    if (errs>1) alert('There are fields which need correction before sending');
    if (errs==1) alert('There is a field which needs correction before sending');
    return (errs==0);

    and was thinking that there has to be a better way of pluralising parts of a string that don't involve lost of duplication, and if so would it be something like this?

    Code javascript:
    // the plural method for strings should have previously been defined
    if (errs) {
        alert('There ' + 'is a field'.plural(errs, 'are fields') +
            ' which ' + 'needs'.plural(errs, 'need') +
            ' correction before sending');
    }
    return !errs;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    When I was loking at his code from the first message, I saw the following

    Code javascript:
    if (errs>1) alert('There are fields which need correction before sending');
    if (errs==1) alert('There is a field which needs correction before sending');
    return (errs==0);

    and was thinking that there has to be a better way of pluralising parts of a string that don't involve lost of duplication, and if so would it be something like this?

    Code javascript:
    // the plural method for strings should have previously been defined
    if (errs) {
        alert('There ' + 'is a field'.plural(errs, 'are fields') +
            ' which ' + 'needs'.plural(errs, 'need') +
            ' correction before sending');
    }
    return !errs;
    Nice thinking.

    Here is another way:

    Code javascript:
    if (errs) {
    	var plural = errs > 1;
        alert('There ' + plural ? 'are fields' : 'is a field' +
            ' which ' + plural ? 'need' : 'needs' +
            ' correction before sending');
    }
    mmj


Bookmarks

Posting Permissions

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