Form Validation question

Events happening in the community are now at Drupal community events on www.drupal.org.
royerd's picture

I have a form I created with an input function that looks like this:

$form['annual_income'] = array(
                '#type' => 'textfield',
                '#required' => TRUE,
                '#title' => t("Your annual income"),
                // Optional description
                '#description' => t('Monthly income times 12'),
               
        );

I want the user to be able to enter a number but not with commas separating the thousands. So 2330 but not 2,330.

Can someone point me in the right direction here to add validation to this form so that it rejects numbers entered with commas and tell the user to input the number without commas?

Any help much appreciated.

Dan

Comments

Element Validation Article

modindigo's picture

http://befused.com/drupal/element-validate

This might help. Some details on element validation, with some specific rules on numeric validation.

Thanks . . . I think I ran

royerd's picture

Thanks . . . I think I ran across this in my research. But I'll study it again.

I might also suggest that you

azinck's picture

I might also suggest that you should just do the user a favor and strip the commas out for them; no need to throw an error!

Yes, I'd love to do that

royerd's picture

Yes, I'd love to do that (strop the commas). I just have to read up on how to create and validate forms I guess.

Try to use jQuery to replace comma

dongtian's picture
  jQuery('#field-node-annual-income-add-more-wrapper input').blur(function() {
jQuery(this).val( jQuery(this).val().replace(/,/g, '') );
  });

or use $form['annual_income']['#element_validate'] = array('your_validate convert_function');
On your function to remove comma.

Thanks for the help!

royerd's picture

Thanks for the help!

Grand Rapids, MI

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: