0

I have a submit form with 4 fields to be populated by a client; "first_name" "last_name" "questions" "administrative_questions" None of the fields can allow number values to be submitted and must return an error if an attempt to post is made with numbers entered. The first two fields are required. The third and fourth field require that one or the other be populated, with no numbers and both can be populated as well but both cannot be blank.

With Ryans guidance, I have the following code in place to deal with the "questions" and "administrative_questions" fields, and it allows one or the other to be blank BUT if both are populated, one will allow numbers to pass through but not if BOTH have numbers or both have number AND text. Only if one has text and one has numbers or numbers and text mixed. The requirements for this form demand that no numbers be allowed to submit. It's a compliance issue with a finance advisory group. Clients can't be allowed to provide account numbers, bank balances or fiscal values of any kind through this web-based submission form and they MUST provide first and last name.

$error_message = "";
$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$questionsEntered = preg_match($string_exp,$questions);

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$administrativeQuestionsEntered = preg_match($string_exp,$administrative_questions);

if ($questionsEntered && $administrativeQuestionsEntered) {
// both were entered - ok?
}
elseif ($questionsEntered || $administrativeQuestionsEntered) {
// either one was entered - ok
}
else {
// explain that at least one of them must be entered 
$error_message = 'your error message';
0

1 Answer 1

1

I hadn't read the instructions as well as i should...

  • The error_message is appended to
  • If the fields are entered then they must be valid (two checks per field)
  • it must report all fields in error.

Here is the new, tested code...

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$questionsEntered = strlen(trim($questions)) > 0;
$questionsValid = preg_match($string_exp, $questions);
$questionErrorMsg = 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Questions for Barbara"</font> you entered does not appear to be Valid,<br />
or contains numbers which are not permitted.<br />
In order to assure your security, the use of numbers is prohibited.<br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$adminQuestionsEntered = strlen(trim($administrative_questions)) > 0;
$adminQuestionsValid = preg_match($string_exp, $administrative_questions);
$adminQuestionsErrorMsg = 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Questions for Ray"</font> you entered does not appear to be Valid,<br />
or contains numbers which are not permitted.<br />
In order to assure your security, the use of numbers is prohibited.<br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';


if ($questionsEntered || $adminQuestionsEntered) {
   // either one or both were entered, are they Valid?

    if ($questionsEntered && !$questionsValid) {
        $error_message .= $questionErrorMsg;
    }

    if ($adminQuestionsEntered && !$adminQuestionsValid) {
        $error_message .= $adminQuestionsErrorMsg;
    }
}
else {
  // explain that at least one of them must be entered
  $error_message .= 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Some questions must be entered"</font><br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes. There are two fields ahead of the "questions" and "administrative_questions" fields that are for "first_name" and "last_name" that are required fields and also demand the prevention of numeric values being added. The error message in the code I put up is for "any error" whether it be illegal character entry or no entry at all. I can modify that for the two fields to be specific to their validations.
I edited the question to clarify the needs and reasons and cleared out my old code in favor of the code you provided me as well as details of my testing results. I hope this helps. Thanks!
This works PERFECTLY!! THANK YOU THANK YOU THANK YOU!! The only piece I am missing now is that the first two fields "first_name_ and "last_name" are required and cannot be left blank. I am going to attempt to modify your provided code but if you happen to see this this morning and feel inclined to help me one last time, I'd be very thankful!!
Ryan Vincent, You are awesome!! I was able to add my original validation routines for "first_name" and "last_name" ahead of your code and all works like magic!! Thank you, AGAIN!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.