I have a question but I can’t find the answer for my problem. Hope you can help me!
Here’s my HTML:
JavaScript
x
4
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<input type="text" id="one">
3
<input type="text" id="two">
4
Here’s my Javascript:
JavaScript
1
8
1
$('#one').on('input', function(){
2
var value = $(this).val();
3
if (value.includes("@") && value.includes(".com"))
4
{
5
$('#two').focus();
6
}
7
});
8
What this script does
I’m making a login form where the input focuses on the second input when a user has typed his email. In my code: it focuses on the next input when the first input detects an ‘@’ symbol and the text ‘.com’.
But here’s the problem
Lets say your email is: ‘john.computer@gmail.com’. My script focuses on the second input after you typed the ‘@’ symbol, because the input already contains ‘.com’ and ‘@’.
My Question
What javascript do I need to add that checks if ‘.com’ is typed after the ‘@’ symbol?
Advertisement
Answer
You could use regex: @.*.com
JavaScript
1
2
1
if (/@.*.com/.test(value))
2