Topic: Javascript validation
CoryT87 free asked 6 years ago
I'm trying to get the "Javascript Validation" to work from the "Contact form" section of this site. I'm using my own Contact form and when I add the javascript script and then test it out I get the "This field is required." but the script isn't working for the subject section of the form. Any help would be greatly appreciated! My code:
<div class="contact-us-info">
<div class="container">
<div class="row">
<div class="col-lg-8 message">
<h3>Send us a message</h3>
<p>
You can contact us with anything related to Devcde. <br/> We'll get in touch with you as soon as possible.
</p>
<form role="form" id="contact-form" action="mail.php" method="POST">
<div class="form-group">
<label for="name">Your name</label>
<input type="text" name="name" class="form-control" id="name" />
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" name="email" class="form-control" id="email" />
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" name="subject" class="form-control" id="subject" />
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea name="message" class="form-control" id="message" rows="6"></textarea>
</div>
<div class="submit">
<input type="submit" class="button button-small" onclick="validateForm()" value="Email us" />
</div>
</form>
<script>
function validateForm() {
var name = document.getElementById('name').value;
if (name == "") {
document.getElementById('status').innerHTML = "Name cannot be empty";
return false;
}
var email = document.getElementById('email').value;
if (email == "") {
document.getElementById('status').innerHTML = "Email cannot be empty";
return false;
} else {
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,}))$/;
if(!re.test(email)){
document.getElementById('status').innerHTML = "Email format invalid";
return false;
}
}
var subject = document.getElementById('subject').value;
if (subject == "") {
document.getElementById('status').innerHTML = "Subject cannot be empty";
return false;
}
var message = document.getElementById('message').value;
if (message == "") {
document.getElementById('status').innerHTML = "Message cannot be empty";
return false;
}
document.getElementById('status').innerHTML = "Sending...";
document.getElementById('contact-form').submit();
}
</script>
Piotr Glejzer staff answered 6 years ago
Hi,
did you try to use our example with files?
Did you create everything like in instructions?
Best,
Piotr
TeddyS31 pro commented 6 years ago
does the javascript form require php?
Bartłomiej Malanowski staff commented 6 years ago
No, it doesn't PHP
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: General Bootstrap questions
- MDB Version: -
- Device: Web
- Browser: Safari
- OS: MacOS
- Provided sample code: Yes
- Provided link: Yes