I need to validate an email address input in JavaScript. The format should be name#xx.xx where "name" is another input text in the form and I have it in a var called "name_input".
I don't know how to be sure that the email will contains that variable before the # sign.
You don't need regex for this. Just String#indexOf will be suffice:
var email = 'name#xx.xx';
var name_input = 'name';
var isValid = (email.indexOf(name_input + '#') == 0);
//=> true
Related
it is possible to grab from email only data until #gmail.com ?
for example:
myemail#gmail.com
I want to select only
myemail
It should be aplycable for any emails. Any length of emails.
if I have emails:
myemail11#gmail.com and myem#gmail.com
it should give me:
myemail11 and myem
I thing req expreson will be good for that , but I am not goog in reg expr.
The simplest way is by getting string before last # by using substring:
const address = "myemail#gmail.com";
const username = address.substring(0, address.lastIndexOf("#"));
console.log(username)
Apart from the substring, you can also split the email and take the first index value.
const email = "myemail#gmail.com";
const name = email.split('#')[0]
console.log(name)
I would like to write a simple if statement to test for special characters. Something like this:
var passwordSpecial = **WHATDOIPUTHERE???**.test(password)
if(passwordSpecial == true){
var pass="OK"
}
else{
var pass="NO"
}
const pattern = new RegExp(/^[^#]+$/);
const password1 = pattern.test("hey")
const password2 = pattern.test("#hey")
//returns true, false
console.log(password1, password2)
anything you put inside the brackets after the "^" won't be allowed in the string, in this case "#"
Edit: see this code pen
https://codepen.io/PavlosKaralis/pen/vYLeYqp?editors=1011
And if you want to specifically only allow numbers and letters
const pattern = new RegExp(/^[a-zA-Z0-9]*$/);
It depends on your definition of what a special character is.
I am new to javaScript and the problem I am facing is that I have to match the # symbol and make sure it is allowed only once in a string. To do so I have written the following regex.
var regexpat=/[#]{1}/;
if(regexpat.test(valu))
testresults = true;
else
{
alert("Please input a valid email address!");
testresults = false;
}
My regex is working for the following input value: abcd#abc#abc.com. However, if I provide the input value as "abcd#"#abc.com it is not throwing the alert error message.
How can I change my regex so that it will work for "abcd#"#abc.com?
Your regexp just tests whether there's a single # in the string, it doesn't reject more than one. Use:
var regexppat = /^[^#]+#[^#]+$/;
This matches an # that's surrounded by characters that aren't #.
var valu;
var regexpat = /^[^#]+#[^#]+$/;
while (valu = prompt("Enter email")) {
if (regexpat.test(valu))
console.log(valu + " is valid");
else {
console.log(valu + " is invalid");
}
}
The easy way could also be to use the split("#") for this:
var value = 'abcd##';
if(value.split("#").length === 2){
testresults = true;
}else{
alert("Please input a valid email address!");
testresults = false;
}
Just split your string with # and since you require only one occurrence of # there must be an array of length 2 so you can compare the array with length 2. If the array length is greater than 2 then there are more than one occurrence of #
E-Mail regex is much more, than just the occurence of just one # character. This is the email-regex specified in the W3C Spec (e.g. used for <input type="email">):
/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
I am having a script which validates a user's name. When the user enters name without spaces then it works fine. If user enters name with spaces (name, surname, lastname etc) then it is not working. I am trying to capture 'space' character using \s but it is not working.
My script is:
var name=document.forms["newform"]["fullname"].value; //get the name from HTML form
var charpos = name.search("[^A-Za-z\s]");
if (name.length <= 0 || name.length>=50 || charpos >= 0)
{
alert('wrong.');
}
else
{
alert('correct');
}
Input1: AFGH
Output1: correct
Input2: ABCD EFGH
Output2: wrong
When Input2 is given, charpos gets the value '5'.
The string literal is swallowing the \.
You should use a regex literal:
name.search(/[^A-Za-z\s]/);
How can I take an e-mail address from "XXX <email#email.com>" ? I don't want to get the "< >".
Thanks!
Here's one based on Tejs' answer. Simple to understand and I think a bit more elegant
// Split on < or >
var parts = "XXX <email#email.com>".split(/[<>]/);
var name = parts[0], email = parts[1];
Really simply (no need for regex!)
var components = emailAddress.split('<')
if(components.length > 1)
{
var emailAddress = components[1].replace('>', '');
}
function getEmailsFromString(input) {
var ret = [];
var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g;
var match;
while ( match = email.exec(input) ) {
ret.push({'name': match[1], 'email': match[2]});
}
return ret;
}
var str = '"Name one" <foo#domain.com>, ..., "And so on" <andsoon#gmx.net>';
var emails = getEmailsFromString(str);
credit: How to find out emails and names out of a string in javascript
^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$
Matches e-mail addresses, including some of the newer top-level-domain extensions, such as info, museum, name, etc. Also allows for emails tied directly to IP addresses.
This regex will work for your example.
/<([^>]+)/
It searches for anything after the '<' that is not a '>' and that is returned in your matches.
To just grab what's inside the angle brackets, you can use the following:
var pattern = /<(.*)>/;
pattern.exec("XXX <foo#bar.com>"); // this returns ["<foo#bar.com>", "foo#bar.com"]
Not positive if I'm understanding you correctly. If you want to get the email domain ie gmail.com or hotmail.com then you could just use
var x =string.indexOf("#"); var y =string.subString(x)
this will give you the string y as the email domain.