need to change email regex in jquery validate plugin [duplicate] - javascript

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 7 years ago.
email: function( value, element ) {
// From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
// Retrieved 2014-01-14
// If you have a problem with this implementation, report a bug against the above spec
// Or use custom methods to implement your own email validation
return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
}
i found above code in jquery validation plugin for email validation here i replaced the regex with others but still it validates email as "example#example" but in my application i want it to validate emails of type "example#example.example" only ,is this regex expression only which needs to be changed or some other part of the code too because changing regex didn't worked for me

Just change the last * (zero or more) to + (one or more) to force at least one .something:
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/
^
# This is the only change ----------------------------------------------------------------------------------------------------------|
Online demo

Related

Regex validation within if statements [duplicate]

This question already has answers here:
Check whether a string matches a regex in JS
(13 answers)
Closed 8 months ago.
Hi I am hoping this may be and easy one for some of you.
Essentially I am just asking if it is possible to use a regex match statement within a if statement.
I have used some in my Formik validation schema but am not sure if it is possible to use within an if statement.
This is my if statement
if (this.state.email.length < 8 || this.state.password.length < 8)
I would like to include something along the logic of
.matches(/(?=.*outlook)/)
Is this possible ?
I think what you're looking for is the regex .test() method. It applies a regex to a string and returns true if it matches, or false if it doesn't. See here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
you can use match to validate regex expression in javascript, this is an example of email validation :
function ValidateEmail(inputText) {
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (inputText.match(mailformat)) return true;
}

User input sometimes breaks REGEX [duplicate]

This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 3 years ago.
I have the following code to filter a list based on what the user types in an input field:
const searchQuery = e.target.value.trim();
items
.filter((item) =>
item.title.match(new RegExp(searchQuery, 'gi'))
)
.map(item => {
// Do something...
});
it works great except everything breaks when the user enters some funky text in the input field like:
//on\/: \/: \
Results in the following error:
SyntaxError: Invalid regular expression: ///on\/: \/: \/: \ at end of pattern
How to approach this correctly so that my code doesn't break based on user input?
Sounds like you need to perform escapes, or some type of validation. Here's a link to the JavaScript escape() Function. I hope that helps.

Email regex returning empty [duplicate]

This question already has answers here:
Extract all email addresses from bulk text using jquery
(8 answers)
Closed 4 years ago.
I am trying to extract multiple email addresses from a string of text using a regex in Zapier code.
var rawList = "This is just a test of everything#test.com not sure how regex#email.com can extract multiple email#addresses.com but we will see";
var emailList = rawList.match(/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,}\b/);
console.log(emailList)
This always returns emailList as null. I pulled this regex expression from https://www.regular-expressions.info/index.html I have also tried Email regular expressions from other websites with still the same experiences.
I have also used Zapier's Formatter's Extract Pattern option and tried the same expression with no luck either. Not sure what is going on here?
Your regex code doesn't work. You should use /(([A-Za-z0-9]+\w*[\.\-]?){1,}\w*#([A-Za-z0-9]+\.){1,2}[A-z]{2,3})/gm.
See test below.
var rawList="This is just a test of everything#test.com not sure how regex#email.com can extract multiple email#addresses.com but we will see";
var emailList=rawList.match(/(([A-Za-z0-9]+\w*[\.\-]?){1,}\w*#([A-Za-z0-9]+\.){1,2}[A-z]{2,3})/gm);
console.log(emailList);

Email validation regex for rails and javascript [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 5 years ago.
I have a regular expression I've adapted from Michael Hartl's rails book that I'm using to validate email addresses
/\A([\w+\-]\.?)+#[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
I want to use this for validation in javascript, but it doesn't work there, and I can't seem to figure out why. I'm just spinning my wheels here
So the above one works in Rails, but doesn't work in JS. I've found other expressions I can use in JS that work, but I want to try to use the same ones to keep it consistent.
For clarification, this is the rails side:
email = "test#test.com"
return /\A([\w+\-]\.?)+#[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
this returns true
and on the JS side:
email = "test#test.com"
return /^\A([\w+\-]\.?)+#[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.test( email );
this returns false
Any pointers?
Per the comment by #Wiktor Stribiżew the missing piece was changing the regex syntax to work with JS, like this
return /^([\w+\-]\.?)+#[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+$/i.test( email );
I took out the \A and \z and replaced them with \^ and $, respectively

Regular expression for check website url [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular expression for URL validation (in JavaScript)
How to validate url?
I have a textbox in a form where user can enter any website url. i want to check that url, whether it is valid or not through regular expression. it will accept only like this "http://www.google.com" or "www.google.co.in".
I have using a expression like
/((http|https|ftp):\/\/(www|WWW)|(www|WWW))\.[A-Za-z]{3}/ .
it is working but when i enter wwww.google.co.in, it says valid.
can any one help me please.
Do a request server side and check for 200 OK. Don't trust the client.
We needed it in our project a couple of weeks ago, and I think we used this one
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
return regexp.test(s);
}
UPDATE:
I'm still working on it:
(ftp|http|https):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+#&#;`~=%!]*)(\.\w{2,})?)*\/?

Categories