Email regex validation javascript - javascript

I do understand the problems with validating emails but I wonder whether this would block anyone that has a legal email.
I was looking for a list of valid emails to test it myself but did not find any.
Anyone have an email that is valid but this regex thinks it's not?
emailRegex.test('Emailing#domain.aero')
Very long line:
emailRegex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(#((?:[\w-]+\.)*\w[\w-]{0,66})\.(([a-z]{2}|AERO|ARPA|ASIA|BIZ|CAT|COM|COOP|EDU|GOV|INFO|INT|JOBS|MIL|MOBI|MUSEUM|NAME|NET|ORG|PRO|TEL|TRAVEL|XN--0ZWM56D|XN--11B5BS3A9AJ6G|XN--3E0B707E|XN--45BRJ9C|XN--80AKHBYKNJ4F|XN--90A3AC|XN--9T4B11YI5A|XN--CLCHC0EA0B2G2A9GCD|XN--DEBA0AD|XN--FIQS8S|XN--FIQZ9S|XN--FPCRJ9C3D|XN--FZC2C9E2C|XN--G6W251D|XN--GECRJ9C|XN--H2BRJ9C|XN--HGBK6AJ7F53BBA|XN--HLCJ6AYA9ESC7A|XN--J6W193G|XN--JXALPDLP|XN--KGBECHTV|XN--KPRW13D|XN--KPRY57D|XN--LGBBAT1AD8J|XN--MGBAAM7A8H|XN--MGBAYH7GPA|XN--MGBBH1A71E|XN--MGBC0A9AZCG|XN--MGBERP4A5D4AR|XN--O3CW4H|XN--OGBPF8FL|XN--P1AI|XN--PGBS0DH|XN--S9BRJ9C|XN--WGBH1C|XN--WGBL6A|XN--XKC2AL3HYE2A|XN--XKC2DL3A5EE0H|XN--YFRO4I67O|XN--YGBI2AMMX|XN--ZCKZAH|XXX)(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)|(^$)/i;

Dominic Sayers has created a list of email edge cases that you could use to validate your test. You can find it here.
The valid address test#[IPv6:::], "test\ test"#iana.org or "test#io" are not accepted by your regex.

It's a beautiful expression, but soon to be relegated to the realm of obsolescence:
http://www.icann.org/en/topics/new-gtld-program.htm
Global Top Level Domains (yourbestfriend#worksfor.coke) are coming, and they'll break all of our scripts in a few years :)
Though, to answer your question, no, I was not able to break your email check using today's finite limit on "valid" domain extensions.

Here is the code for html input field and button field
<input input type="text" name="txtEmailId" id="txtEmailId" />
<input type="submit" class="button" value="Suscribe" name="Suscribe"
onclick="javascript:ShowAlert()" />
Now add the below function to the header of your page
<script type="text/javascript">
function ShowAlert() {
var email = document.getElementById('txtEmailId');
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
else {
alert("Thanks for your intrest in us, Now you
will be able to receive monthly updates from us.");
document.getElementById('txtEmailId').value = "";
}
}
</script>
Here you can find the article on this Email Validation in JavaScript

Related

How To Hide All But One Character From A Password Field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I have done quite a bit of research on this question, but have found no definitive answer. Essentially, on a users' account page, I want to be able to display their password with all but the first letter hidden. It would look like this:
Password: p*******
The password is going to be pulled as a normal string from a database, then needs to be hidden before being placed into the appropriate text field. How can I accomplish this in javascript?
Added Information
I would like to have the password stored in a normal <p> or <h3> Tag--not editable by the user until requested (I'll handle that).
All passwords are currently being stored in a firebase database.
As many others have stated, it is not advisable to do this with JavaScript. Doing this with JavaScript requires the password to be sent with plain text which is visible to anyone snooping on the user's network. It would be much better to hide the characters on the server first and, then, send the result to the browser. Doing it entirely with JavaScript is a HUGE security risk. Think carefully before doing it this way.
This answer assumes you want to a variable to store the user's actual password for later use. The variable password holds the actual password in it. The textbox only holds the first letter while all other characters are represented by *.
window.addEventListener('load',function(){
var passwordBox = document.getElementById('password');
var password;
passwordBox.addEventListener('input',function(){
password = this.value;
this.value = '';
var characters = password.split('');
if(characters.length > 0){
this.value = characters[0];
for(var i = 1; i < characters.length; i++){
this.value += '*';
}
}
});
});
<input type="text" id="password">
The second last thing you should do is passing users' passwords as plain text. The third last thing you should do is replacing password characters with javascript. Both of these allow unauthorized persons to reveal your user's password. But the very last thing you should do is storing users' passwords in your DB in plain form, which allows unathorized persons to obtain all of your users' passwords.
Even if you replace password characters on the server side and send it back in "p*******" form as the others have suggested, you give clear signal that you store your users' passwords in not-encrypted form, thus your site is more likely to be attacked.
Please have in mind that most, if not all bundles/frameworks provide user management support including at least basic password encryption by default, just like FOSUserBundle.
String masking
If you simply have a password string, and want the string with asterisks:
function mask(str){
return str.split("").map((e,i)=>i?"*":e).join("");
}
Use like this:
alert(mask("supersecretpass"));
Input masking
window.onload=function(){
var passelem=;//add your input elem here
var password="";//the real password
passelem.oninput=function(){
if(password.length>this.value.length){
//user pressed backspace...
var arr=password.split("");
arr.pop();//remove last char
password=arr.join("");
return;
}
password+=this.value.split("").pop();//add added char to password
this.value=password.split("")[0];//add first char
for(i=1;i<password.length;i++){
this.value+="*";//add the *
}
};
//kill arrows
document.onkeydown=function(e){
if(e.wich==33||e.wich==34){
e.preventDefault();
return false;
}
};
};
Like www139s answer, but with correct password output...

zf2 Form message in a tooltip : where does it come from?

Let's say I have a form with an email form element;
when I write for instance 1 letter in this field and I press OK, I get a message in a tooltip, in my language (french) :
"Veuillez saisir une adresse courriel valide" (something like "Please insert a valid email adress" in english !).
I'd like to know where does this message come from. I thought from javascript/bootstrap : no result.
Thank you
In addition to the comment from #dan-klasson:
When you use \Zend\Form\Element\Mail (or EMail?) ZF will render this as <input type="email">. This email field is handled and validated by the browser - like date, range and what-ever other new html5 elements exists.
If you think of replacing you validating to html5-browser validation - it's not a good approach, because other browsers could handle different mails different.

how to make a regex string reject a blank field [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 8 years ago.
I am very new to coding so go easy.
I am trying to make a email validation form but it needs to reject a blank cell (input box) sorry for being so bad at coding..... i also was going to use a regex
it has to be alpanumeric#alpanumeric.alpanumeric
sorry
The correct behavior in this case would be to perform a "pre-check" on fields before actually executing some more complex validation (eg: regular expressions).
The logic would look something like this:
valid_email = false;
email = strip_leading_trailing_spaces( email ); // don't forget to cleanup user input
if ( email != "" ) {
// perform regex testing here, set valid_email to false if failed
}
// handle "valid_email" variable here
It's worth noting here that any client side validation should be duplicated to/re-checked on the server as any user with a little knowledge in JS could easily bypass any validation done on the clients computer.
You don't need a regex if you are just checking to see if it's empty:
<input type=text id=email><button onClick="validate()">Validate</button>
<script language="javascript">
function validate() {
if ($("#email").val().length == 0) {
alert("Enter an email address");
}
}
</script>
with a regexp :
var valid_email = ! email.match( /^\s*$/ ) ;
If there's only spaces and tabs or nothing then valid_email = false
demo : http://regex101.com/r/iX8lF7/1

Create Regex Pattern from Mask value to validate the zip code

JS Code:
I was using the following code for zip code validation for three countries USA/BR/CA.
/*To Get ZipCodeRegex as per country */
function getZipRegEx(inputcountry) {
var returntext;
if (inputcountry == "USA") {
returntext = /^\d{5}$/
}
else if (inputcountry == "CA") {
returntext = /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$/
}
else if (inputcountry == "BR") {
returntext = /^([0-9]){5}([-])([0-9]){3}$/
}
return returntext;
};
Now, client has updated their code and using the MASK Plugin for input textbox. The mask pattern is rendering with data-mask attribute. Below is the rendered HTML for input control for zip code.
<input type="text" name="zip" id="zip" value="" class="required" data-mask="99999" />
Problem Area : Now with latest code base, they are covering more then 10 countries and each and every country/state is having different pattern for zip code.
So, is it possible to generate the regex pattern based on mask value coming with input field? This way, i need not to put the country specific checks.
Please suggest some implementation approach to cover this scenario.
Thanks in advance.
The MASK Plugin is already performing client-side validation of the entered ZIP code. That is what it is for. It doesn't make sense to duplicate that with your own client-side validation using Javascript.
Client side validation is just a convenience for the user anyway (reporting mistakes quickly so the user can correct them before the form is submitted); it never guarantees valid input, because it can be easily circumvented.
If you want to ensure good input, you must validate on the server side.

regular expression in email validation javascript

I am doing email regular expression validation for the email and I am stuck on one point using
following expression if user enter something like abc.abc then this expression works fine but when user enters advxyz#pqr.com then it doesn't work
var myreg = new RegExp(/([a-z0-9.]+)#([a-z0-9]+)\.([a-z0-9]+)/i);
var patter = myreg.test(document.getElementById("email").value)
alert(patter)
if(patter == false){
errorMsg.push("email Formate Error Ex:firstname.lastname#abc.com");
}
I want that user must enter his email in this formate like firstname.lastname#abc.com/.ca/.org
var pattern = /^[a-z0-9]+\.[a-z0-9]+#[a-z0-9]+\.[a-z]+$/i;
pattern.test('firstname.lastname#abc.com');
// returns true;
pattern.test('jsmith#abc.com');
// returns false;
pattern.test("abc.abc");
// returns false;
Using Regular Expressions is probably the best way.
Here's an example
(live demo):
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\ ".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
Source
This work Perfectly for me.
/^[a-zA-Z]([a-zA-Z0-9_\-])+([\.][a-zA-Z0-9_]+)*\#((([a-zA-Z0-9\-])+\.){1,2})([a-zA-Z0-9]{2,40})$/;
Hope this is helpful in resolving the issue \b[A-Z0-9._%-]+#[A-Z0-9.-]+.[A-Z]{2,4}\b
Its case sensitive.
Pattern for to validate email address is:
/^([a-zA-Z0-9_.-])+\#(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
use this pattern in Regex. and you can validate any email..

Categories