4

I am using the jquery validate function and plugin located here. http://docs.jquery.com/Plugins/Validation

I am going thrue the js file and i have found the email validation block that makes sure it is a valid email, the thing is, i want to only allow .edu emails. I have no idea where to start.

3 Answers3

7

You can extend it and add your own rule.

jQuery.validator.addMethod("edu", function(value, element) {
  // Make it work optionally OR
  //  Check the last 4 characters and ensure they match .edu
  return (this.optional(element) || value.slice(-4) == ".edu"); 
}, "You must use a .edu email address");

$("#myform").validate({
  rules: {
    field: {
      required: true,
      email: true,
      edu: true
    }
  }
});

Here is a jsfiddle showing how it all works together and how the errors cascade. Just press the button.

Brombomb
  • 6,988
  • 4
  • 38
  • 57
6

You don't need custom extension. You can use accept() method: http://docs.jquery.com/Plugins/Validation/Methods/accept#extension

// input field
<input type="text" id="eduEmail2" name="eduEmail2" value="abc@123.edu"/>

// validation rule
eduEmail2: {
    required: true,
    email: true,
    accept: 'edu'
}

Full example: http://jsfiddle.net/M5TmU/2/ (based on Brombomb's jsfiddle)

UPDATE

After xecute's comment - you may use custom regex pattern. In additional-methods.js you have "pattern" method (last one in a file):

eduEmail3: {
    required: true,
    email: true,
    pattern: /(\.edu\.\w\w)$/
}

Full example: http://jsfiddle.net/M5TmU/3/

Of course it's not perfect, but still - you don't need write custom method :)

ADDITIONAL EDIT: country domain names have two letters, so \w\w\w? is too much - \w\w should do. I left \w\w\w? in fiddle

BartekR
  • 3,827
  • 3
  • 24
  • 33
  • What about localization? "edu.fr", "edu.tr", "edu.ru", "edu.de"..? They are university students too. – xecute Jan 12 '12 at 15:21
  • EXACTLY what I was looking for. Bravo! – Joe Feb 02 '12 at 22:57
  • Wow this is truly awesome! – Connor Finn McKelvey Dec 22 '12 at 16:09
  • [As per the documentation: the `accept` method _"makes a file upload accept only specified mime-types"_](http://jqueryvalidation.org/accept-method). ~ How does that have anything to do with the OP's question? – Sparky Jan 06 '15 at 21:52
  • @Sparky: link also mentions: "Note: This method used to look at just the filename, specifically the file extension. That behaviour is now available as the "extension" method inside src/additional/extension.js."; at the time of writing this was the case – BartekR Jan 07 '15 at 09:25
  • I don't understand what you're trying to tell me. Both the `accept` and `extension` methods are used for validating a **file upload while using the `type="upload"` input**. This was also the case in 2012. Point being, what does validation of a file upload have to do with a `type="text"` input? – Sparky Jan 07 '15 at 15:42
  • You're right, I was not percise. I remeber that when I answered 11Jan2012 `accept` parameter wasn't only for file inputs. I checked github repository, version 1.9.0, `accept` method is in `jquery-validate.js` file. It's not bound to file input, it's just another validation method. It was changed in April 2012 as I'm looking at https://github.com/jzaefferer/jquery-validation/issues/287 – BartekR Jan 08 '15 at 09:20
0

You probably will still get spam from non .edu email address.

I would suggest checking the email domain server side.

So that the email has to be .edu or it won't mail.

Otherwise if I turn off my JavaScript in my browser I can submit any email address and it will send.

Blynn
  • 1,411
  • 7
  • 27
  • 48
  • He just want to show people the e-mail without .edu extension is not allowed. Of course you have to control again this things server side if you are doing JS filtering or etc. Without server side controlling, it's not acceptable. – xecute Jan 12 '12 at 15:19