Progressive validation of email address via RegEx in Javascript










0














I'd like to do progressive validation of an email address. By that I mean I would like to validate, as a string is being provided one character at a time, whether or not the current string represents a valid beginning of an email address.



Note that I'm aware of this and other similar answers that provide excellent patterns for matching a complete email address. What I'm looking for is slightly different.



I'd like to know, given a regex pattern, say the below pattern as described in the link above, if there's a general way to say if a given string represents a valid beginning of the pattern.






/^(([^<>()[]\.,;: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,))$/





I understand that I can manually decompose the above pattern into composite sections and "OR" them together for longer pattern captures from the beginning of the main pattern forward, but I'm hoping there's something a little more elegant and/or a little less verbose that could just reference the established pattern as a capture group and look inside for a partial matches of the beginning only. Is this possible to achieve with regular expressions?



Strings the regex would match:



  • ""

  • "test"

  • "test.user"

  • "test.user."

  • "test.user.1@"

  • "test.user.1@test"

  • "test.user.1@test.best"

  • "test.user.1@test.best.com"

Strings the regex would not match:



  • "@#$@#"

  • "test.."

  • "test.user.@"

  • "test.user.1@@"

  • "test.user.1@test..best"

  • "test.user.1@test.best@"









share|improve this question





















  • This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
    – deceze
    Nov 13 '18 at 0:20















0














I'd like to do progressive validation of an email address. By that I mean I would like to validate, as a string is being provided one character at a time, whether or not the current string represents a valid beginning of an email address.



Note that I'm aware of this and other similar answers that provide excellent patterns for matching a complete email address. What I'm looking for is slightly different.



I'd like to know, given a regex pattern, say the below pattern as described in the link above, if there's a general way to say if a given string represents a valid beginning of the pattern.






/^(([^<>()[]\.,;: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,))$/





I understand that I can manually decompose the above pattern into composite sections and "OR" them together for longer pattern captures from the beginning of the main pattern forward, but I'm hoping there's something a little more elegant and/or a little less verbose that could just reference the established pattern as a capture group and look inside for a partial matches of the beginning only. Is this possible to achieve with regular expressions?



Strings the regex would match:



  • ""

  • "test"

  • "test.user"

  • "test.user."

  • "test.user.1@"

  • "test.user.1@test"

  • "test.user.1@test.best"

  • "test.user.1@test.best.com"

Strings the regex would not match:



  • "@#$@#"

  • "test.."

  • "test.user.@"

  • "test.user.1@@"

  • "test.user.1@test..best"

  • "test.user.1@test.best@"









share|improve this question





















  • This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
    – deceze
    Nov 13 '18 at 0:20













0












0








0







I'd like to do progressive validation of an email address. By that I mean I would like to validate, as a string is being provided one character at a time, whether or not the current string represents a valid beginning of an email address.



Note that I'm aware of this and other similar answers that provide excellent patterns for matching a complete email address. What I'm looking for is slightly different.



I'd like to know, given a regex pattern, say the below pattern as described in the link above, if there's a general way to say if a given string represents a valid beginning of the pattern.






/^(([^<>()[]\.,;: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,))$/





I understand that I can manually decompose the above pattern into composite sections and "OR" them together for longer pattern captures from the beginning of the main pattern forward, but I'm hoping there's something a little more elegant and/or a little less verbose that could just reference the established pattern as a capture group and look inside for a partial matches of the beginning only. Is this possible to achieve with regular expressions?



Strings the regex would match:



  • ""

  • "test"

  • "test.user"

  • "test.user."

  • "test.user.1@"

  • "test.user.1@test"

  • "test.user.1@test.best"

  • "test.user.1@test.best.com"

Strings the regex would not match:



  • "@#$@#"

  • "test.."

  • "test.user.@"

  • "test.user.1@@"

  • "test.user.1@test..best"

  • "test.user.1@test.best@"









share|improve this question













I'd like to do progressive validation of an email address. By that I mean I would like to validate, as a string is being provided one character at a time, whether or not the current string represents a valid beginning of an email address.



Note that I'm aware of this and other similar answers that provide excellent patterns for matching a complete email address. What I'm looking for is slightly different.



I'd like to know, given a regex pattern, say the below pattern as described in the link above, if there's a general way to say if a given string represents a valid beginning of the pattern.






/^(([^<>()[]\.,;: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,))$/





I understand that I can manually decompose the above pattern into composite sections and "OR" them together for longer pattern captures from the beginning of the main pattern forward, but I'm hoping there's something a little more elegant and/or a little less verbose that could just reference the established pattern as a capture group and look inside for a partial matches of the beginning only. Is this possible to achieve with regular expressions?



Strings the regex would match:



  • ""

  • "test"

  • "test.user"

  • "test.user."

  • "test.user.1@"

  • "test.user.1@test"

  • "test.user.1@test.best"

  • "test.user.1@test.best.com"

Strings the regex would not match:



  • "@#$@#"

  • "test.."

  • "test.user.@"

  • "test.user.1@@"

  • "test.user.1@test..best"

  • "test.user.1@test.best@"





/^(([^<>()[]\.,;: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,))$/





/^(([^<>()[]\.,;: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,))$/






javascript regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 23:59









jdmcnairjdmcnair

1,0901130




1,0901130











  • This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
    – deceze
    Nov 13 '18 at 0:20
















  • This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
    – deceze
    Nov 13 '18 at 0:20















This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
– deceze
Nov 13 '18 at 0:20




This is tough with a regex, but easy with a parser. Start parsing the string, and throw an exception or such once you encounter a syntax error. If the parser simply falls off the end without error, it's okay. Obviously you'd need to have separate return values for "not invalid but incomplete" and "complete and valid" too.
– deceze
Nov 13 '18 at 0:20












1 Answer
1






active

oldest

votes


















3














I'm simplifying the email pattern to /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ to simplify the step of the method.



  1. You identify a scenario for entering a valid value with a pattern:


Value regex matches
----------------- ----------------------------------------- ---------
t /^[a-z]+/ Yes
to /^[a-z]+/ Yes
tom /^[a-z]+/ Yes
tom. /^[a-z]+./ Yes
tom.e /^[a-z]+(.[a-z]+)*/ Yes
tom.ed /^[a-z]+(.[a-z]+)*/ Yes
tom.ed@ /^[a-z]+(.[a-z]+)*@/ Yes
tom.ed@i /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@in /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@int /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inte /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter. /^[a-z]+(.[a-z]+)*@[a-z]+./ Yes
tom.ed@inter.n /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.ne /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.net /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ Yes



  1. Then you look for ways to combine a pattern that only match 1 line with the previous pattern.



    • For /^[a-z]+./, we can combine it with /^[a-z]+/ and obtain /^[a-z]+.?/.

    • For /^[a-z]+(.*[a-z]+)*@/, we can combine it with /^[a-z]+(.*[a-z]+)*/ and obtain /^[a-z]+(.*[a-z]+)*@?/.

    • For /^[a-z]+(.*[a-z]+)*@[a-z]+./, we can combine it with /^[a-z]+(.*[a-z]+)*@[a-z]+/ and obtain /^[a-z]+(.*[a-z]+)*@[a-z]+.?/.

    • ...


You continue until you are satisfied that you have covered all possible case.



  1. You put everything together using this /^( ... | ... | ... | ... )/. Here a possible regex you would obtain :

/^([a-z]+.?|[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@|[a-z]+(.[a-z]+)*@[a-z]+.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+)$/



Her is a better view :



( [a-z]+.?
| [a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@
| [a-z]+(.[a-z]+)*@[a-z]+.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+ )


  1. Last you test to see if it will allows unwanted values

You can do that at regex101.com or in a test page.



Note: This pattern is only good while the user enters a value. Before submitting, the original pattern (/^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/) must be used to validate the value so that no incomplete values are sent to the server.



EDIT :
To make it more readable and maintainable, you can do



var patt1 = "[a-z]+.?";
var patt2 = "[a-z]+(.[a-z]+)*@";
var patt3 = "[a-z]+(.[a-z]+)*@[a-z]+.?";
var patt4 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?";
var patt5 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+";

var keypressedValidator = new RegExp("^("+patt1+"|"+"+patt2+"|"+"+patt3+"|"+"+patt4+"|"+"+patt5+")$");

...

var inputValue = document.getElementById(...some Id...).value;

if ( ! inputValue.match(keypressedValidator)) {
... show error status or error message ...
...





share|improve this answer






















  • This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
    – jdmcnair
    Nov 13 '18 at 15:26










  • Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
    – jdmcnair
    Nov 13 '18 at 15:54










  • @jdmcnair I'v added a code example to make it more readable and maintainable.
    – Dominique Fortin
    Nov 13 '18 at 21:24










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271851%2fprogressive-validation-of-email-address-via-regex-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I'm simplifying the email pattern to /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ to simplify the step of the method.



  1. You identify a scenario for entering a valid value with a pattern:


Value regex matches
----------------- ----------------------------------------- ---------
t /^[a-z]+/ Yes
to /^[a-z]+/ Yes
tom /^[a-z]+/ Yes
tom. /^[a-z]+./ Yes
tom.e /^[a-z]+(.[a-z]+)*/ Yes
tom.ed /^[a-z]+(.[a-z]+)*/ Yes
tom.ed@ /^[a-z]+(.[a-z]+)*@/ Yes
tom.ed@i /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@in /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@int /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inte /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter. /^[a-z]+(.[a-z]+)*@[a-z]+./ Yes
tom.ed@inter.n /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.ne /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.net /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ Yes



  1. Then you look for ways to combine a pattern that only match 1 line with the previous pattern.



    • For /^[a-z]+./, we can combine it with /^[a-z]+/ and obtain /^[a-z]+.?/.

    • For /^[a-z]+(.*[a-z]+)*@/, we can combine it with /^[a-z]+(.*[a-z]+)*/ and obtain /^[a-z]+(.*[a-z]+)*@?/.

    • For /^[a-z]+(.*[a-z]+)*@[a-z]+./, we can combine it with /^[a-z]+(.*[a-z]+)*@[a-z]+/ and obtain /^[a-z]+(.*[a-z]+)*@[a-z]+.?/.

    • ...


You continue until you are satisfied that you have covered all possible case.



  1. You put everything together using this /^( ... | ... | ... | ... )/. Here a possible regex you would obtain :

/^([a-z]+.?|[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@|[a-z]+(.[a-z]+)*@[a-z]+.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+)$/



Her is a better view :



( [a-z]+.?
| [a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@
| [a-z]+(.[a-z]+)*@[a-z]+.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+ )


  1. Last you test to see if it will allows unwanted values

You can do that at regex101.com or in a test page.



Note: This pattern is only good while the user enters a value. Before submitting, the original pattern (/^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/) must be used to validate the value so that no incomplete values are sent to the server.



EDIT :
To make it more readable and maintainable, you can do



var patt1 = "[a-z]+.?";
var patt2 = "[a-z]+(.[a-z]+)*@";
var patt3 = "[a-z]+(.[a-z]+)*@[a-z]+.?";
var patt4 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?";
var patt5 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+";

var keypressedValidator = new RegExp("^("+patt1+"|"+"+patt2+"|"+"+patt3+"|"+"+patt4+"|"+"+patt5+")$");

...

var inputValue = document.getElementById(...some Id...).value;

if ( ! inputValue.match(keypressedValidator)) {
... show error status or error message ...
...





share|improve this answer






















  • This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
    – jdmcnair
    Nov 13 '18 at 15:26










  • Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
    – jdmcnair
    Nov 13 '18 at 15:54










  • @jdmcnair I'v added a code example to make it more readable and maintainable.
    – Dominique Fortin
    Nov 13 '18 at 21:24















3














I'm simplifying the email pattern to /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ to simplify the step of the method.



  1. You identify a scenario for entering a valid value with a pattern:


Value regex matches
----------------- ----------------------------------------- ---------
t /^[a-z]+/ Yes
to /^[a-z]+/ Yes
tom /^[a-z]+/ Yes
tom. /^[a-z]+./ Yes
tom.e /^[a-z]+(.[a-z]+)*/ Yes
tom.ed /^[a-z]+(.[a-z]+)*/ Yes
tom.ed@ /^[a-z]+(.[a-z]+)*@/ Yes
tom.ed@i /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@in /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@int /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inte /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter. /^[a-z]+(.[a-z]+)*@[a-z]+./ Yes
tom.ed@inter.n /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.ne /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.net /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ Yes



  1. Then you look for ways to combine a pattern that only match 1 line with the previous pattern.



    • For /^[a-z]+./, we can combine it with /^[a-z]+/ and obtain /^[a-z]+.?/.

    • For /^[a-z]+(.*[a-z]+)*@/, we can combine it with /^[a-z]+(.*[a-z]+)*/ and obtain /^[a-z]+(.*[a-z]+)*@?/.

    • For /^[a-z]+(.*[a-z]+)*@[a-z]+./, we can combine it with /^[a-z]+(.*[a-z]+)*@[a-z]+/ and obtain /^[a-z]+(.*[a-z]+)*@[a-z]+.?/.

    • ...


You continue until you are satisfied that you have covered all possible case.



  1. You put everything together using this /^( ... | ... | ... | ... )/. Here a possible regex you would obtain :

/^([a-z]+.?|[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@|[a-z]+(.[a-z]+)*@[a-z]+.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+)$/



Her is a better view :



( [a-z]+.?
| [a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@
| [a-z]+(.[a-z]+)*@[a-z]+.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+ )


  1. Last you test to see if it will allows unwanted values

You can do that at regex101.com or in a test page.



Note: This pattern is only good while the user enters a value. Before submitting, the original pattern (/^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/) must be used to validate the value so that no incomplete values are sent to the server.



EDIT :
To make it more readable and maintainable, you can do



var patt1 = "[a-z]+.?";
var patt2 = "[a-z]+(.[a-z]+)*@";
var patt3 = "[a-z]+(.[a-z]+)*@[a-z]+.?";
var patt4 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?";
var patt5 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+";

var keypressedValidator = new RegExp("^("+patt1+"|"+"+patt2+"|"+"+patt3+"|"+"+patt4+"|"+"+patt5+")$");

...

var inputValue = document.getElementById(...some Id...).value;

if ( ! inputValue.match(keypressedValidator)) {
... show error status or error message ...
...





share|improve this answer






















  • This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
    – jdmcnair
    Nov 13 '18 at 15:26










  • Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
    – jdmcnair
    Nov 13 '18 at 15:54










  • @jdmcnair I'v added a code example to make it more readable and maintainable.
    – Dominique Fortin
    Nov 13 '18 at 21:24













3












3








3






I'm simplifying the email pattern to /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ to simplify the step of the method.



  1. You identify a scenario for entering a valid value with a pattern:


Value regex matches
----------------- ----------------------------------------- ---------
t /^[a-z]+/ Yes
to /^[a-z]+/ Yes
tom /^[a-z]+/ Yes
tom. /^[a-z]+./ Yes
tom.e /^[a-z]+(.[a-z]+)*/ Yes
tom.ed /^[a-z]+(.[a-z]+)*/ Yes
tom.ed@ /^[a-z]+(.[a-z]+)*@/ Yes
tom.ed@i /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@in /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@int /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inte /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter. /^[a-z]+(.[a-z]+)*@[a-z]+./ Yes
tom.ed@inter.n /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.ne /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.net /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ Yes



  1. Then you look for ways to combine a pattern that only match 1 line with the previous pattern.



    • For /^[a-z]+./, we can combine it with /^[a-z]+/ and obtain /^[a-z]+.?/.

    • For /^[a-z]+(.*[a-z]+)*@/, we can combine it with /^[a-z]+(.*[a-z]+)*/ and obtain /^[a-z]+(.*[a-z]+)*@?/.

    • For /^[a-z]+(.*[a-z]+)*@[a-z]+./, we can combine it with /^[a-z]+(.*[a-z]+)*@[a-z]+/ and obtain /^[a-z]+(.*[a-z]+)*@[a-z]+.?/.

    • ...


You continue until you are satisfied that you have covered all possible case.



  1. You put everything together using this /^( ... | ... | ... | ... )/. Here a possible regex you would obtain :

/^([a-z]+.?|[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@|[a-z]+(.[a-z]+)*@[a-z]+.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+)$/



Her is a better view :



( [a-z]+.?
| [a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@
| [a-z]+(.[a-z]+)*@[a-z]+.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+ )


  1. Last you test to see if it will allows unwanted values

You can do that at regex101.com or in a test page.



Note: This pattern is only good while the user enters a value. Before submitting, the original pattern (/^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/) must be used to validate the value so that no incomplete values are sent to the server.



EDIT :
To make it more readable and maintainable, you can do



var patt1 = "[a-z]+.?";
var patt2 = "[a-z]+(.[a-z]+)*@";
var patt3 = "[a-z]+(.[a-z]+)*@[a-z]+.?";
var patt4 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?";
var patt5 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+";

var keypressedValidator = new RegExp("^("+patt1+"|"+"+patt2+"|"+"+patt3+"|"+"+patt4+"|"+"+patt5+")$");

...

var inputValue = document.getElementById(...some Id...).value;

if ( ! inputValue.match(keypressedValidator)) {
... show error status or error message ...
...





share|improve this answer














I'm simplifying the email pattern to /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ to simplify the step of the method.



  1. You identify a scenario for entering a valid value with a pattern:


Value regex matches
----------------- ----------------------------------------- ---------
t /^[a-z]+/ Yes
to /^[a-z]+/ Yes
tom /^[a-z]+/ Yes
tom. /^[a-z]+./ Yes
tom.e /^[a-z]+(.[a-z]+)*/ Yes
tom.ed /^[a-z]+(.[a-z]+)*/ Yes
tom.ed@ /^[a-z]+(.[a-z]+)*@/ Yes
tom.ed@i /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@in /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@int /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inte /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter /^[a-z]+(.[a-z]+)*@[a-z]+/ Yes
tom.ed@inter. /^[a-z]+(.[a-z]+)*@[a-z]+./ Yes
tom.ed@inter.n /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.ne /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+/ Yes
tom.ed@inter.net /^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/ Yes



  1. Then you look for ways to combine a pattern that only match 1 line with the previous pattern.



    • For /^[a-z]+./, we can combine it with /^[a-z]+/ and obtain /^[a-z]+.?/.

    • For /^[a-z]+(.*[a-z]+)*@/, we can combine it with /^[a-z]+(.*[a-z]+)*/ and obtain /^[a-z]+(.*[a-z]+)*@?/.

    • For /^[a-z]+(.*[a-z]+)*@[a-z]+./, we can combine it with /^[a-z]+(.*[a-z]+)*@[a-z]+/ and obtain /^[a-z]+(.*[a-z]+)*@[a-z]+.?/.

    • ...


You continue until you are satisfied that you have covered all possible case.



  1. You put everything together using this /^( ... | ... | ... | ... )/. Here a possible regex you would obtain :

/^([a-z]+.?|[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@|[a-z]+(.[a-z]+)*@[a-z]+.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?|[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+)$/



Her is a better view :



( [a-z]+.?
| [a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@
| [a-z]+(.[a-z]+)*@[a-z]+.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?
| [a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+ )


  1. Last you test to see if it will allows unwanted values

You can do that at regex101.com or in a test page.



Note: This pattern is only good while the user enters a value. Before submitting, the original pattern (/^[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+$/) must be used to validate the value so that no incomplete values are sent to the server.



EDIT :
To make it more readable and maintainable, you can do



var patt1 = "[a-z]+.?";
var patt2 = "[a-z]+(.[a-z]+)*@";
var patt3 = "[a-z]+(.[a-z]+)*@[a-z]+.?";
var patt4 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)*.?";
var patt5 = "[a-z]+(.[a-z]+)*@[a-z]+(.[a-z]+)+";

var keypressedValidator = new RegExp("^("+patt1+"|"+"+patt2+"|"+"+patt3+"|"+"+patt4+"|"+"+patt5+")$");

...

var inputValue = document.getElementById(...some Id...).value;

if ( ! inputValue.match(keypressedValidator)) {
... show error status or error message ...
...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 22:06

























answered Nov 13 '18 at 1:06









Dominique FortinDominique Fortin

1,640816




1,640816











  • This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
    – jdmcnair
    Nov 13 '18 at 15:26










  • Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
    – jdmcnair
    Nov 13 '18 at 15:54










  • @jdmcnair I'v added a code example to make it more readable and maintainable.
    – Dominique Fortin
    Nov 13 '18 at 21:24
















  • This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
    – jdmcnair
    Nov 13 '18 at 15:26










  • Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
    – jdmcnair
    Nov 13 '18 at 15:54










  • @jdmcnair I'v added a code example to make it more readable and maintainable.
    – Dominique Fortin
    Nov 13 '18 at 21:24















This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
– jdmcnair
Nov 13 '18 at 15:26




This is pretty much that "manually decompose the above pattern into composite sections and "OR" them together" option I was talking about above. I think it will work, but it'll also be extremely verbose and difficult to maintain for anything aside from the most simplified notion of an email address pattern. I'm asking for something a little more specific; is there a way to take an original pattern, make it a capture group, and use some kind of self-referential operation to determine if a given string is a valid beginning of the original pattern.
– jdmcnair
Nov 13 '18 at 15:26












Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
– jdmcnair
Nov 13 '18 at 15:54




Note that it's OK and not altogether unexpected if the answer is "no, that doesn't exist", but I'm going for something fairly specific.
– jdmcnair
Nov 13 '18 at 15:54












@jdmcnair I'v added a code example to make it more readable and maintainable.
– Dominique Fortin
Nov 13 '18 at 21:24




@jdmcnair I'v added a code example to make it more readable and maintainable.
– Dominique Fortin
Nov 13 '18 at 21:24

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271851%2fprogressive-validation-of-email-address-via-regex-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto