Regex validation within if statements [duplicate] - javascript

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;
}

Related

3 Character Long Alphanumeric Regex Not Working [duplicate]

This question already has answers here:
How to detect exact length in regex
(8 answers)
Closed 4 years ago.
So I am trying to use a regular expression to check against strings but it doesn't seem to be working properly.
Basically I want it to match a alpha-numeric string that is exactly 3 characters long. The expression I am using below does not seem to be working for this:
const msg = message.content;
const regex = /[A-Za-z0-9]{3}/g;
if (msg.match(regex)) {
// Do something
}
Am I doing something wrong? Any help would be appreciated. Thanks in advance.
You need to add ^ and $ for the start-of-string anchor and end-of-string anchor, respectively - otherwise, for example, for #123, the 123 will match, and it will pass the regex. You also might consider using the i flag rather than repeat A-Za-z, and you can use \d instead of 0-9.
It looks like you just want to check whether the string passes the regex's test or not, in which case .test (evaluates to a boolean) might be a bit more appropriate than .match. Also, either way, there's no need for the global flag if you're just checking whether a string passes a regex:
const regex = /^[a-z\d]{3}$/i;
if (regex.test(msg)) {
// do something
}

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);

Matching exactly 10 digits in Javascript [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
I've tried other questions on SO but they don't seem to provide a solution to my problem:
I have the following simplified Validate function
function Validate() {
var pattern = new RegExp("([^\d])\d{10}([^\d])");
if (pattern.test(document.getElementById('PersonIdentifier').value)) {
return true;
}
else {
return false;
}
}
I've tested to see if the value is retrieved properly which it is. But it doesn't match exactly 10 digits. I don't want any more or less. only accept 10 digits otherwise return false.
I can't get it to work. Have tried to tweak the pattern in several ways, but can't get it right. Maybe the problem is elsewhere?
I've had success with the following in C#:
Regex pattern = new Regex(#"(?<!\d)\d{10}(?!\d)")
Examples of what is acceptable:
0123456789
,1478589654
,1425366989
Not acceptable:
a123456789
,123456789a
,a12345678a
You can try with test() function that returns true/false
var str='0123456789';
console.log(/^\d{10}$/.test(str));
OR with String#match() function that returns null if not matched
var str='0123456789';
console.log(str.match(/^\d{10}$/));
Note: Just use ^ and $ to match whole string.
You can use this:
var pattern = /^[0-9]{10}$/;
You can try this :
var str = "0123456789";
var pattern = new RegExp("^[0-9]{10}$");
pattern.test(str);

What is the JavaScript equivalent of preg_match? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can i use preg_match in jQuery?
What is the jquery equivalent of the PHP preg_match feature?
In PHP it would be :
preg_match('/[^a-zA-Z0-9]/', $str);
Which checks if the string has anything other than letters and numbers.
I'd like to add some client sided validation to my site, but I've looked and looked and can't quite find the jQuery equivalent of this.
Thanks.
In plain JavaScript (no jQuery needed for this), you would just use the .match() method of the string object which will return null if no matches and an array if there are matches:
var str = "myteststring";
if (str.match(/[^a-zA-Z0-9]/)) {
// contains illegal characters
}
not jQuery but JavaScript
var myStr = "something";
/[^a-zA-Z0-9]/.test(myStr) // will return true or false.
or for clarity
var regEx=/[^a-zA-Z0-9]/;
regEx.test(myStr)
The test method is part of the RegEx object... Here is some reference
http://www.w3schools.com/jsref/jsref_regexp_test.asp
If you want to match on a string, you can do it with vanilla JS:
var str = "here's my string";
var matches = str.match('/[^a-zA-Z0-9]/');
If you're trying to do it on a selector, and using jQuery, you can use:
$("div:match('/[^a-zA-Z0-9]/')")

Identify a URL Using Regular Expressions [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Checking for a valid url using Javascript Regular Expressions
PHP validation/regex for URL
I have a if statement that will check if the user entered a URL(HTTP Protocol only), like this:
if(/^regexp/.test(url))
But how should be this regular expression to check if the text is a URL or not?
I believe this little function might help:
function isURL(string){
regEx = /(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim;
return regEx.test(string));
}
Let us know if it worked out!
W.

Categories