I have a nagging feeling this has been asked, but I'm not sure how to phrase it correctly, so I'll ask here anyway.
I'm writing a simple calculator script (for learning, I'm newbie), and I split it up into two parts - front end form validation with JS and calculation with PHP.
To be more specific, here are the three parts:
HTML (index.php)
<form action = "calculator.php" method = "post">
First number: Input field here
Operator: Drop down
Second number: Input field here
Submit button
</form>
JavaScript (validate.js)
validate() - regex checks that first and second number are all digits
PHP (calculator.php)
Extract first and second number, calculate result, and display on page
Here's my question - I would like to make it such that when the submit button is pressed, the page is passed on to the server ONLY if all regex tests are passed.
Something like:
validate() {
// testing code
...
if first_num_valid == true && second_num_valid == true
redirect to calculator.php
}
How do I do this in Javascript?
Or, if this isn't possible, can you suggest an alternative to this?
Thanks!
EDIT 1: Tried @joe's solution but not sure how to make it work?
$("#calculator").onsubmit(function() {
if (first_valid && second_valid)
return true;
else
return false;
});
I assume @joe was trying to say that the form only submit only if the return value is true, but I tried this, but the form submitted whether or not the values were true?
EDIT 2: For clarification
index.php
<form id = "calculator" action = "./calculation.php" method = "post" >
<table>
<tr>
<td>First number</td>
<td><input type = "text" name = "first_num" id = "first_num"/></td>
</tr>
<tr>
<td>Operator</td>
<td>
<select name = "operator">
<option value = "+" name = "add">+</option>
<option value = "-" name = "minus">-</option>
<option value = "*" name = "multiply">*</option>
<option value = "/" name = "divide">/</option>
</select>
</td>
</tr>
<tr>
<td>Second number</td>
<td><input type = "text" name = "second_num" id = "second_num" /></td>
</tr>
<tr>
<td>
<input type = "submit" name = "submit" id = "submit" onclick = "validate()"/>
</td>
</tr>
</table>
</form>
validate.js
function validate() {
var first_num = $("#first_num").val();
var second_num = $("#second_num").val();
var regex = new RegExp("^[\\d]+$");
var first_valid = regex.test(first_num);
var second_valid = regex.test(second_num);
var alert_string = "";
if (!first_valid && second_valid)
alert_string += "First number invalid!<br />";
else if (first_valid && !second_valid)
alert_string += "Second number invalid!";
else if (!first_valid && !second_valid)
alert_string += "Both numbers invalid!";
if (!first_valid || !second_valid)
alert(alert_string);
$("#calculator").onsubmit(function() {
if (first_valid && second_valid)
return true;
else
return false;
});
}
EDIT 3: Put all code in handler, didn't work?
$("#calculator").on('submit', function() {
var first_num = $("#first_num").val();
var second_num = $("#second_num").val();
var regex = new RegExp("^[\\d]+$");
var first_valid = regex.test(first_num);
var second_valid = regex.test(second_num);
var alert_string = "";
if (!first_valid && second_valid)
alert_string += "First number invalid!";
else if (first_valid && !second_valid)
alert_string += "Second number invalid!";
else if (!first_valid && !second_valid)
alert_string += "Both numbers invalid!";
if (!first_valid || !second_valid)
alert(alert_string);
if (first_valid && second_valid)
return true;
else
return false;
});