2

Upon new user registration, how could I validate that the email address has a ".edu" extension? Basically, I need to ensure that users are using only their ".edu" email addresses to register for accounts.

1

Try something like this, add a validate to user_registration_form and then in validate check email with your pattern and if not match use form_set_error. something like below

function registration_check_form_user_register_alter(&$form, &$form_state)
{
    $form['#validate'][] = 'registration_check_user_register_validate';
}
function registration_check_user_register_validate($form, &$form_state)
{
        $email=$form_state['values']['email']['und'][0];
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)\.edu/i",$email))
                {
                form_set_error('mail', t('Email NOT VALID,TRY EMAIL WITH .EDU'));
            }   
}
1

It's not ideal, but I ended up writing a Javascript based solution. Essentially, it looks for the string .edu in the email address on blur of the email field and click of the submit button. If it doesn't find it, it prevents the form from being submitted. It looks something like this:

<script type="text/javascript">
jQuery(document).ready(function() {

    function eduCheck() {
        if (jQuery('#edit-mail').val()) {
            if (jQuery('#edit-mail').val().indexOf('.edu') != -1) {
                // alert('Edu present');
            } else {
                alert('Please use a .edu email address only.');
                return false;
            };
        };
    }

    jQuery('#edit-mail').blur(function() {
        eduCheck();
    });

    jQuery('#edit-submit').click(function() {
        eduCheck();
    });

});
</script>

Like I said, it's not ideal. But it's a suitable alternative... especially since I couldn't get any of the other suggestions on this page to work.

0

To start, you can use the Rules Forms Module (dependent on the Rules Module)

You can state specific conditions during specific events with specific outcomes. I would also suggest installing the Token Module if you don't already have it.

You can of course do this manually by making a tiny module that validates the User Account form on submission with a simple stripos() like so:

if(stripos($emailField, '.edu', strlen($emailField) - 4) > 3) {
    // Success...
} else {
    // Failed - email doesn't end in .edu
}

This of course uses $emailField as the submitted field containing the user's e-mail address.

  • Thanks! Can you expand a bit on how one might use the Rules Forms Module. I've got it installed. I've got it set on the event "Before saving a user account", but now I'm stumped; none of the available actions seem relevant. – Sam Feb 22 '14 at 3:09

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.