0

Looking for way to prevent the submission of fields containing certain domains or strings from being accepted into form that sends the data to database.

Searches all send me to strpos.

Can produce a message but not stop the form from submitting when selected URL or string is in field.

Below is form entry box using email field as example:

<input type="email" name="email"  required >

A variable is made from the post.

<?php
$email = test_input($_POST["email"]);
?>

I can get it to give a message by using this code:

<?php
if ($email contains "selected.domain")
   echo "email address cannot be this domain";
?>

but need it to prevent submission if email address contains "selected.domain", or any specified string.

Not a programmer but . . . .

This is the type of form:

<form name="submissionsform" action="" method="post" >

This is the email input and Event URL input box. Both need to have certain domains restricted. That's the issue. Tried the strpos code but so far can't get it to work as needed. These are already required fields, just need to add the exemption for certain domains or strings. Hopefully this makes more sense.

<center>Your email:<br><input type="email" name="email" style=" border-radius:5px; width:90%; height:25px" required></center>

<center>Event Link - IMPORTANT:<br><input type="url" name="link" value=""  style=" border-radius:5px; width:95%; height:25px" required></center>

The Submit button:

<center> <button style=" border-radius:5px; background-color:transparent; width:100%" name="save_event_data">Submit</button></center>

2 Answers2

0

can use strpos() to check str containing certain characters, check this question :

How do I check if a string contains a specific word?

0

Could you just return false in your php code?

i.e.

<?php
// $email contains domain
if (strpos($email, 'selected@domain') !== false)
   echo "email address cannot be this domain";
   return false; // this will end the code block early if email contains the domain
?>

I don't know your use case, but you can do it in javascript as well.

in your html:

// on submitting form, check your validation function
<form onsubmit="return validate()">
    <input type="email" name="email" id="email" required >
...// rest of your form

<script>
    // check for invalid domain
    function validate() {
        if(document.getElementById("email").value.includes('selected@domain')) {
            alert('email contains foo');
            return false;
        }
    }
</script>
Michael Peng
  • 806
  • 9
  • 11
  • Not trying to verify. Needing to prevent submission if email address contains specified string or URL. User must not be able to submit form if email address contains designated string or URL. – user1935030 Apr 03 '19 at 01:40
  • Are you able to do this in javascript? Not sure of your requirements, but validate() function i provided does just that.It will prevent the form from submitting a post/get . – Michael Peng Apr 03 '19 at 02:03
  • No. The form is in php and MySQL. There is much more to the form than shown here. – user1935030 Apr 03 '19 at 02:11
  • The field in question is a required field that needs to exclude certain URLs. We are not checking to see if domain is valid. We know it is. When a user inputs an email or URL it cannot contain certain domains or strings. This has to happen before the form is submitted just like other required fields. – user1935030 Apr 03 '19 at 02:18
  • Right now, if incorrectly formatted input is done in the existing required fields, the form will not submit. For instance, if the email address is not in email format, the form will take you back to the field in question when attempting to submit. This also needs to happen when the field contains a given string or URL. I can't just return false because that doesn't prevent submission. – user1935030 Apr 03 '19 at 02:28