0

The custom function formdvalidate should run after else but it doesn't. How can I trigger it to run? What I like to accomplish here is to check the text-fields of a form. If there are less then 3 characters its show the user some text. If the input is more then 3 it submits the form.

--I was thinking to wrong way, this works better.

$('#submit').click(formdvalidate);

The function

function formdvalidate(){
    $(".form-sel").each(function(){
        var n = $(this).closest('.contain-form').find('.custom:first').val().length;
        if ($('option:selected', this).val() === 'custom' && n < 3) {
            $(this).closest('.contain-form').find('.alert-short:first').show();
        }else{                          
            $('#poll').submit();
        }
    });                 
}
2
  • 1
    A click handler will never get true as an argument. Commented Jun 14, 2011 at 14:15
  • I see. Didn't know that. Its seem like I have to change a lot more to make this code work. Commented Jun 14, 2011 at 14:25

5 Answers 5

3
  1. A function assigned as a click handler via .click() takes a jQuery event object as its first param--you named that param the same as your function
  2. formdvalidate === true will never evaluate to true as formdvalidate is a function in the global scope, and a jQuery event object in the click() scope as per item 1
  3. In your formdvalidate function, your return is inside of the .each() function, so formdvalidate never actually returns anything.

I'm not sure what you're really after or what your page looks like, but based on your comment to Slava Yanson, my best guess for you is below (although there are still things I am unsure of...)

$( '#submit' ).click( function( e )
{
  if( formdvalidate() === true) 
  {
    $( '#poll' ).submit();
  }

  e.preventDefault();
} );

function formdvalidate()
{
  var returnValue;

  $( '.form-sel' ).each( function()
  {
    var $containForm = $( this ).closest( '.contain-form' ),
        $alertShortFirst = $containForm.find( '.alert-short:first' ),
        n = $containForm.find( '.custom:first' ).val().length;
    if( $( 'option:selected', this ).val() === 'custom' && n < 3 )
    {
      $alertShortFirst.show();
      returnValue = false;
    }
    else
    {
      $alertShortFirst.hide();
      returnValue = true;
    }
  } );

  return returnValue;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The formdvalidate parameter is hiding the formdvalidate function.

Use better names.

Comments

1

Try

$('#submit').click(function() {
 if (formdvalidate() === true)  {
     $('#poll').submit();
        } else {        
             formdvalidate();
        }

});

I don't know if your logic will accomplish what you're trying to though.

2 Comments

Agreed--that would seem the most obvious edit, and yet we have no way of knowing if it is desired. Especially since it doesn't seem to make sense to run formdvalidate again after it failed.
@JAAulde "Especially since it doesn't seem to make sense to run formdvalidate again after it failed." exactly what I was thinking
0

On line 2 of your code you are comparing an instance of your function to boolean. That won't validate. Can you provide more information on what you are trying to accomplish?

1 Comment

In essence the functions should show a text when the input is less then 3 characters.
0

Your argument "formvalidate" is shadowing your "formvalidate" function. Just rename your argument to something like this:

$('#submit').click(function(do_validate) {
 if (do_validate === true)  {
     $('#poll').submit();
        } else {        
             formdvalidate();
        }
});

1 Comment

do_validate will never be === true as it is a jQuery event object (first param to a click handling function which was assigned via .click()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.