php - issue with validating non required input with jquery
Get the solution ↓↓↓Solution:
I'll begin with your Javascript code:
else if (maiden_name !== '' && maiden_name !== null) {
if (!maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
alert("Mother maiden name not properly entered!");
}
First you should verify if the value is empty then do your code:
var mom_maiden_name = $('#moms_md_name').val();
if (mom_maiden_name == '' || mom_maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
//Validation passed
} else {
//Validation failed again
}
I'm not sure what the regex should do but I assume it's correct.
Second, looking at your PHP code, I see a wrong validation there. If you first check a value for not being empty, you can automatically be assured that it's set. If you want to keep your code, you should invert those 2 conditions in 'if' :
if(isset($_POST['moms_md_name'])) {
if (!empty($_POST['moms_md_name']) && !preg_match('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['moms_md_name'])) {
echo "Mother maiden name need to be properly entered!";
exit();
}
}
Again, I don't know what the regex does so I will assume it's correct.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.