I can give only required and maxlength validation in webform fields , How can i give

alphabets or numbers only validation in webform textfields ??


I used hook like this :-

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'webform_client_form_48') {
        if (!preg_match('/^[A-Za-z]+$/', $form['submitted']['name']['#value'])) {
            // $form['submitted'][$key]['#attributes']['class'] = 'error';
            form_set_error('name', t('Please enter alphabets only for Name.'));

this is working . but the order of displaying message is not proper , the first three

message is coming from ajax form validation and fourth one is from the hook , but i need

the fourth message should come first .

enter image description here

  • 1
    Unless there's an option to validate by regular expression, you'll probably need to write a small module which hooks into the form validation and checks – Clive Mar 12 '15 at 10:45
  • Yes , regular expression option is not available in webforms , can you provide some code suggestion – KTM Mar 12 '15 at 10:49
  • 1
    It would be a generic hook_form_alter(), add a validation handler, validate the field. There are dozens and dozens of examples online (and probably on this site). Looks like you won't need to bother though from Krishna's answer – Clive Mar 12 '15 at 10:51
  • Sorry my mistake , i already using this method like this and displaying the error messages too , but the problem is specifically the error message is displaying not in order , it is appearing at last . – KTM Mar 12 '15 at 11:58
  • Would it be more accurate to say your question is "how do I make sure form error messages are rendered in the same order as the fields on the form?". It'd be good to clarify, and that would be an interesting question I think – Clive Mar 12 '15 at 15:52

If you want to go for a module, Try the Webform Validation module

This module adds an extra tab to each webform node, allowing you to specify validation rules for your webform components. You can create one or more of the predefined validation rules, and select which webform component(s) should be validated against those. By using the hooks provided by this module, you can also define your own validation rules in your own modules.

The following validation rules are currently included:

  • Numeric values (optionally specify min and/or max value)
  • Minimum length
  • Maximum length
  • Minimum number of words
  • Maximum number of words
  • Equal values on multiple fields
  • Unique values on multiple fields
  • Specific value
  • Require at least one of two fields
  • Require at least one of several fields
  • Minimum number of selections required
  • Maximum number of selections allowed
  • Exact number of selections required
  • Plain text (disallow tags)
  • Regular expression
  • Must be empty (anti-spam: Hide with CSS)
  • Words blacklist
  • Must match a username
  • This option is not usefull since am using php 5.2.7 . and this is compatible with php 5.3 pnly – KTM Mar 12 '15 at 10:52
  • @Icecreamsandwich I don't think that would be a problem? why don't you update your PHP version I'm afraid. By using the hooks provided by this module, you can also define your own validation rules in your own modules. – Krishna Mohan Mar 12 '15 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.