Testing valid mail address using exec - javascript

I have wrote the below function to validate the email address entered by user.
function checkEmail(v_email)
{
var l_ret=true;
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
if (l_reg.exec(v_email)==null){l_ret=false;}
return l_ret;
}
It works perfectly fine.. with lowercase email address, (example.myemail#example.com) but if it detect any capital letter then it fails. like (example.MYemail#example.com).
I am trying to make it work for both capital as well as lower letter but i am not able to make it.. any one good with regex..could please suggest.
thank you in advance...
regards,
Mona..

Simply use the case insensitive mode:
/^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
^
It makes [a-z] also match uppercase letters. Otherwise, you can use [A-Za-z] to mean both upper and lowercase characters in regex.

Noooooooooooo you DONT use regex to validate EMAIL..
An Email is valid if you can send a mail to it..
To validate Email,follow these steps..
1>Send Mail to that email address in which you can put an activation code or even a link.
2>If you receive the respose,email is valid..
At max your regex should be
^[^#]+#[^#]+$
Stop Validating Email Addresses With Complicated Regular Expressions

Change your regex like this:
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
instead of
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/;

Make the regex ignore the case by adding the flag i like below:
var l_reg = /^[a-z0-9._-]+#[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;

Related

Validating name in visual studio code

var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
while (userName.includes('.')) {
console.log ("Invalid Name!");
var userName = input.question('Please enter your name: '); //Asking User to enter their Name.
}
Above code will ask the user his/her name and store it in "userName". Then it will validate using .includes to check unwanted characters and numbers.
I want to validate if userName has numbers or unwanted characters such as "?/.,;'[]{}|&^%#" etc. I have tried using .includes and validate if a name has "." However, I'm not sure how to go about from there to validate the rest.
After the while checks that it contains the unwanted characters, it will re-prompt the user to enter a new name and it will check again until it returns false.
Is there a solution to this?
You can use REGEX to search for non-alphabetic or space characters in the string:
userName.search(/^[a-zA-Z\s]+$/)
The response will be 0 or -1. 0 means that no characters except A-Z, a-z and space were found, -1 means the contrary.
Edit: I found similar question with more detailed answers here.
let chars = /[$&+,:;=?##|'<>.^*()%!-]/g;
chars.test(UserName)
Use Regular expressions ( Regex )
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

regex email pattern in a negate way

I was working around some regex pattern
I have a variable,
var url = "last_name=Ahuja&something#test.com";
The url contains emailId. I have a regex pattern to check if the variable contains emailId.
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
My requirement is:
I want exact negate(opposite) of the above pattern. Like, My condition should be false if the url contains email pattern.
I mean the regex pattern should be in that way.
Can somebody please help me on this.
Instead of negating your regex, you can test whether it matches.
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var url = "last_name=Ahuja&something#test.com";
if(-1 === url.search(filter))
{
alert("Didn't find it");
}
else
{
alert("Found it");
}
Using a negative lookahead you can check if the string does not have an email at the beginning, which mimics the behavior you want since your regex had start and end anchors.
^(?!([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+).*$
But if you wanted to make sure there was no valid email anywhere in the line, you could modify this a little.
^((?!([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+).)*$
See an explanation here
But you should just do this in code like Jonathan suggests.

How to match that using a JavaScript regex?

This is my code:
var name = 'somename';
var pass = '123somen456';
var regex = new RegExp('.*' + pass + '.*', 'i');
alert(name.match(regex));
The regex just wont match, what I dont understand. Whats wrong here? I want to have a match as soon as any part of name is contained in pass, as long as that match is at least 4 chars long. Example:
som --> no match
some --> match
Thanks!
This regex requires that there are any amount of any character, then 123somen456, and then any amount of any character. name.match(regex) will not return anything because name does not contain the string 123somen456.
To test regular expressions, I recommend using http://regexpal.com/.
It sounds, you may need to apply some algorithms like this or this.
If it is possible using regex in javascript, I'm interested to know.
sorry buddy, I have no option to comment.

Regex expression to match the First url after a space followed

I want to match the First url followed by a space using regex expression while typing in the input box.
For example :
if I type www.google.com it should be matched only after a space followed by the url
ie www.google.com<SPACE>
Code
$(".site").keyup(function()
{
var site=$(this).val();
var exp = /^http(s?):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
var find = site.match(exp);
var url = find? find[0] : null;
if (url === null){
var exp = /[-\w]+(\.[a-z]{2,})+(\S+)?(\/|\/[\w#!:.?+=&%#!\-\/])?/g;
var find = site.match(exp);
url = find? 'http://'+find[0] : null;
}
});
Fiddle
Please help, Thanks in advance
you should be using a better regex to correctly match the query & fragment parts of your url. Have a look here (What is the best regular expression to check if a string is a valid URL?) for a correct IRI/URI structured Regex test.
But here's a rudimentary version:
var regex = /[-\w]+(\.[a-z]{2,})+(\/?)([^\s]+)/g;
var text = 'test google.com/?q=foo basdasd www.url.com/test?q=asdasd#cheese something else';
console.log(text.match(regex));
Expected Result:
["google.com/?q=foo", "www.url.com/test?q=asdasd#cheese"]
If you really want to check for URLs, make sure you include scheme, port, username & password checks just to be safe.
In the context of what you're trying to achieve, you should really put in some delay so that you don't impact browser performance. Regex tests can be expensive when you use complex rules especially so when running the same rule every time a new character is entered. Just think about what you're trying to achieve and whether or not there's a better solution to get there.
With a lookahead:
var exp = /[-\w]+(\.[a-z]{2,})+(\S+)?(\/|\/[\w#!:.?+=&%#!\-\/])?(?= )/g;
I only added this "(?= )" to your regex.
Fiddle

Javascript Validation - Letters and Space

Good day. I'm very new to Javascript. I have this expression: var firstname = /^[A-Za-z]+$/; to validate first name in a form. However, this code only allows the user to input a single name without any space. How about if the user has two or more given first name?
I would like to know how to validate letters and space. I have searched similar questions here in Stackoverflow, but none of the answers worked for me.
I have tried these codes which I have found here: /^[A-Za-z ]$/ or this one /^[a-zA-Z\s]*$/ but these didn't work. Still, whenever I input two names in the First Name field, my alert message prompts me that I have entered an invalid character.
Thanks.
There is a problem with each one that you posted for what you want:
var firstname = /^[A-Za-z]+$/; should be var firstname = /^[A-Za-z ]+$/; (added space)
/^[A-Za-z ]$/ should be /^[A-Za-z ]+$/ (added +)
/^[a-zA-Z\s]*$/ is a problem because I assume from the other examples that you want at least one letter, so it should be /^[a-zA-Z\s]+$/ (*replaced * with +)

Categories

Resources