For the ‘onsubmit’ I have tried ‘onclick’, but it still does not work. Also my JavaScript should be fine, but if there are any errors in general, just let me know. One of my friends told me it has something to do with the type="submit" ,but I do not know a work around to this problem. I moved the ‘onsubmit’ to the top instead of where the submit button is when I changed it from ‘onclick’ to ‘onsubmit’.
function Signup() {
vfname = document.forms["register_form"]['fname'].value;
vlname = document.forms["register_form"]['lname'].value;
vemail = document.forms["register_form"]['email'].value;
vphone = document.forms["register_form"]['phone'].value;
vpassw = document.forms["register_form"]['passw'].value;
vpassc = document.forms["register_form"]['passc'].value;
}
if (Name(vfname, vlname)) {
if (Email(vemail)) {
if (Phone(vphone)) {
if (Password(vpassw, vpassc)) {
alert("Verify success");
register_form.submit();
return true;
}
}
}
}
function Password(vpassw, vpassc) {
var passwordmatch = /^[A-Za-z0-9s]+$/;
if (vpassw.match(passwordmatch) = vpassc.match(passwordmatch)) {
if (vpassw.match(passwordmatch) == null) {
alert("Must enter a password");
return false;
} else {
alert("Passwords match");
return true;
}
} else {
alert("Password error");
return false;
}
}
function Phone(vphone) {
var phoneformat = /^[0-9]+$/;
if (vphone.match(phoneformat) == null) {
alert("Numbers only");
return false;
} else {
if (vphone.length != 10) {
alert("Phone number is not the required length");
} else {
return true;
}
}
}
function Email(vemail) {
var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
if (vemail.match(mailformat)) {
return true;
} else {
alert("Email address does not follow the format");
return false;
}
}
function Name(vfname, vlname) {
var nameformat = /^[a-zA-Z]+$/;
if (vfname.match(nameformat) == null || vlname.match(nameformat) == null) {
alert("First name and last name can only contain letters");
return false;
} else {
return true;
}
}
<script type="text/javascript" src="cue/js/veri.js"></script>
<form name="register_form" method="post" action="http://tlocalhost/ds/signup.php" onsubmit="return Signup();">
<p class="info">First Name</p>
<div class="form-group">
<input id="fname" name="fname" placeholder="John" class="form-control" required>
</div>
<p class="info">Surname</p>
<div class="form-group">
<input id="lname" name="lname" placeholder="Smith" class="form-control" required>
</div>
<p class="info">Email</p>
<div class="form-group">
<input type="email" name="email" placeholder="[email protected]" class="form-control" required>
</div>
<p class="info">Phone Number</p>
<div class="form-group">
<input type="phone" name="phone" placeholder="0412456789" class="form-control" required>
</div>
<p class="info">Password</p>
<div class="form-group">
<input id="passw" name="passw" placeholder="$0aP2S" class="form-control" required>
</div>
<p class="info">Confirm Password</p>
<div class="form-group">
<input id="passc" name="passc" placeholder="$0aP2S" class="form-control" required>
</div>
<div class="form-btn">
<input type="reset" class="btn-reset" value="RESET"></input>
<input type="submit" value="SUBMIT" class="btn-submit"></input>
</div>
<div>
<p class="link"><a href="http://localhost/ds/html-2column.php"> Got an account? Login here! </a></p>
</div>
</form>
2
Answers
Several errors
}
in the wrong place. I simplified the test===
is comparison, you have=
in your match passwordI added some IDs and use the IDs instead of names. Form name is already deprecated.
I also made the functions consts and use recommended eventListeners
Lastly I changed the test to test NOT correct and stop submission instead of testing correct and allow it.
This is the modified part.
You can change this.