JQuery form validation, allow only .EDU addresses

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

I am going to transfer a js file and I found an email validation unit that ensures that it has a valid email address. The thing is, I want to allow only .edu emails. I don’t know where to start.

+4
source share
3 answers

You do not need to configure the extension. You can use the 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 jsfiddle)

UPDATE

After the xecute comment, you can use your own regex pattern. In additional-methods.js you have the " pattern " method (the last in the file):

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

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

Of course, this is not ideal, but still - you do not need to write your own method :)

ADDITIONAL IMAGE : country domain names have two letters, so \w\w\w? too much - \w\w . Did I leave \w\w\w? in the violin

+6
source

You can expand 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 error cascades are. Just click the button.

+7
source

You will probably still receive spam from your email address.

I would suggest checking the email domain server.

So the email must be .edu or it will not be sent.

Otherwise, if I disable my JavaScript in my browser, I can send any email address and send it.

0
source

Source: https://habr.com/ru/post/1388701/


All Articles