+ 2

email restriction using javascript

So basically i would like to know a function how to restrict email validation if provided email is ending with .co in javascript. for example-if i input data@data.co it should show alert message or pop up message any other emails is accepted.

8th Jul 2021, 10:39 AM
Eric Kazhus
Eric Kazhus - avatar
11 Respuestas
+ 5
Eric Kazhus I corrected your HTML code as I already here mentioned and changed the function for validation. The result is as follows: https://code.sololearn.com/WQ6DeAXZFY71/?ref=app
8th Jul 2021, 11:41 AM
JaScript
JaScript - avatar
+ 3
Eric Kazhus more better will be if you save you attempt in SL Playground and test it if it works. Then with your question you can provide the link to your code. After will more easier to help you. By the way, you can use HTML5 for the first check of an email. And the second with your function.
8th Jul 2021, 4:05 PM
JaScript
JaScript - avatar
+ 2
line 16 html replace that with <body onLoad='document.testForm.email.focus()'></body> Target form element by name value. If you have ecent of submit on form you must have to submit it, and for that you should add a submit button put this in line 28 to add submit button <input type="submit" value="submit">
9th Jul 2021, 9:27 AM
Divya Mohan
Divya Mohan - avatar
+ 1
Write a function on input type mail. On change With satememt var mail = document.querySelector(".mail").value ; //get input value var check = /.co$/.test(mail); console.log(check);
8th Jul 2021, 10:57 AM
Divya Mohan
Divya Mohan - avatar
+ 1
HTML <form method="POST" name="testForm" action="thanks.html" onsubmit="return validateForm();"> <input id="myInput" type="text" name="email" placeholder="Type your email address here…" > Javascript var email = document.querySelector(".mail").value ; //get input value var check = /.co$/.test(email); console.log(check); am I missing something?
8th Jul 2021, 11:26 AM
Eric Kazhus
Eric Kazhus - avatar
+ 1
Yes in input tag its id so get element value by id HTML <form method="POST" name="testForm" action="thanks.html" onsubmit="return validateForm();"> <input id="myInput" type="text" name="email" placeholder="Type your email address here…" > Javascript document.querySelector("#myinput").onchange = ()=>{ var mail = document.querySelector("#myinput").value ; var check = /.co$/.test(email); console.log(check); }
8th Jul 2021, 11:54 AM
Divya Mohan
Divya Mohan - avatar
+ 1
Thanks guys for Your answers, I tried all that You suggested but i dont know what I am doing wrong because nothing worked and I tried in different ways. so code is something like that- https://code.sololearn.com/W89A6a16A14a in javascript i have 2 options already and need third one which have to restrict adding email which is ending with .co.
9th Jul 2021, 9:07 AM
Eric Kazhus
Eric Kazhus - avatar
+ 1
Also add else for first if condition to verify that email is correct
9th Jul 2021, 9:33 AM
Divya Mohan
Divya Mohan - avatar
+ 1
Thanks guys for Your help. I really appreciate it. You really helped me.
9th Jul 2021, 3:35 PM
Eric Kazhus
Eric Kazhus - avatar
0
Give me please HTML code
10th Jul 2021, 5:23 AM
Tun Tun
0
Tun Tun, You can see it here. I corrected your HTML code as I already here mentioned and changed the function for validation. The result is as follows: https://code.sololearn.com/WQ6DeAXZFY71/?ref=app
10th Jul 2021, 5:26 AM
Eric Kazhus
Eric Kazhus - avatar