Hey so I am really new to HTML and Javascript. I got a task today to code an Email validation script. I think that my script should be fine but for some reason the website doesn't do anything when I click on my submit button. Here is my script:
HTML Form:
<form class="contact-form" action="contact.html" method="post">
<input type="text" class="contact-form-text" placeholder="Your name*" value="" id="name">
<input type="email" class="contact-form-text" placeholder="Your email*" id="email">
<input type="text" class="contact-form-text" placeholder="Your phone*" id="nummer">
<textarea class="contact-form-text" placeholder="Your message"></textarea>
<p id="pflicht">* sind Pflichtfelder</p>
<input type="button" class="contact-form-btn" value="Send" onclick="multiFunction()">
</form>
And this is my function:
function checkforEM(value){
var laenge = value.length;//6
if(value.indexOf("@") > 0){
alert("@ ist da und nicht ganz vorne");
}
else{
alert("@ ist nicht da oder auf Pos 1");
}
// das @ darf nicht am Ende sein
// Frage: wie finde ich das raus?
// Ich prüfe, ob der Indexwert von @ == 5 ist
if(value.indexOf("@") == laenge - 1){ //5
alert("@ ist am Ende");
}
if(value.substring(value.length-3) == ".de"){
alert("Ist eine .de-Top-Level-Domain");
}else{
alert("Ist keine .de-Top-Level-Domain");
}
}
I am using a function that combines mutiple functions at a time and it looks like this:
function multiFunction(){
checkforBlank();
checkforNu();
checkforNa();
checkforEM(this.value);
}
Thanks in advance!
If you use input type="email"
, then the email validation is done inside the browser and you do not need to write your own code for it. And to require a form field to be entered use the required
attribute.
<input type="email" class="contact-form-text" placeholder="Your email*" id="email" required>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.