0

I'm creating a basic subscribe form for a system that requires both email address and last name (SugarCRM in case anyone is interested).

I need to populate the last name input value with something (anything, it doesn't matter) only if the visitor doesn't put their own last name in.

As the subscribe action is hosted (Mail Chimp) I don't have access to it to pre-validate, unless this can happen before it's passed on. I'm wading into deeper PHP waters than I'm comfortable with! Is this possible with jQuery?

So basically I'm getting around the required last name by filling the value with something.

gdoron
  • 147,333
  • 58
  • 291
  • 367
Cordial
  • 539
  • 3
  • 7
  • 22

2 Answers2

3

Check the lastname on submit of the form, if it doesn't have value, put "foo" in it:

$('#formId').submit(function() { // attach submit handler to the form.
    var $lastName = $('#lastName'); // get the last name element.
    if (!$lastName.val())   // if it doesn't have value.
        $lastName.val('foo'); // put foo as a value.
});​

You should replace formId and lastName to the actual ids of the form and last name element.

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Yes, this is for sure possible with jQuery. Just look at the jQuery API and then register an event to the submit button, so, that the event is executed before the submit action. There are several possibilities.

Using selectors you can search your last name input field and read and compare the entered value. If needed you can then change the value of your input field.

Just take a look at http://api.jquery.com/. Study there the events, selectors and manipulation values. Examples are pretty well self explaining.

elisman
  • 143
  • 1
  • 1
  • 8
  • You were faster. :-) As I wrote the answer, yours was not there. But it's a better one! :-) Thanks. – elisman Apr 12 '12 at 10:48