0

Here is my header for my form.

<form action="/page" id="landing_form" method="post" style="top:<?php echo get_post_meta($p->ID, 'top', true); ?>px; left:<?php echo get_post_meta($p->ID, 'left', true); ?>px;"  >

And a simple text field within my form, and submit button

<label>Email</label><input type="text" value="" id="landing_email" name="email" >
<input type="submit" name="submit" value="<?php echo get_post_meta($p->ID, 'button', true); ?>" class="landing_submit" id="landing_submit">

Now how would I make a function that checks if the email has a "."/"@" in it? Or is a specific string.

I have tried in my header to put something like onSubmit="return function('email);" and then putting the function within the file.

However it doesn't even check the function, and just submits the form regardless.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
k9b
  • 1,457
  • 4
  • 24
  • 54
  • If I understand correctly you actually want *client side* validation with JavaScript. e.g. you don't want the form to be submitted to the server until the data meets your needs. (this doesn't exclude server side validation needs... but what you want right now is browser based validation - correct?) – scunliffe May 22 '14 at 18:44

2 Answers2

0

Assuming you want client-side validation you will have to create a javascript function like in this post

Therefore you will need your method:

<script>
function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 
function validate(email) {
    if (!validateEmail(email)) {
        return false;
    }

}
</script>

Then on the form you insert:

onSubmit="validate(email);"

This should not allow the form submission if the email is not valid. Please be aware that you will have to validate the email on the server side also.

Community
  • 1
  • 1
0

the "onSubmit" attribute calls a piece of javascript when the submit button is pressed. It doesn't interact with your PHP at all.

What you'll probably want in your "page" page is something like the following:

<?php 
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];
        if(strpos($email, '@') && strpos($email, '.') && strpos($email, 'some specific string'))
        {
            // This code executes
            // only if there is an @
            // and a full stop and "some specific string"
            // inside the email
        }
    }
?>

Strpos is a function that searches for a substring within a string and returns the position of its first occurrence. If it cannot find the substring, it returns false.

  • Now would there be any way to "Kick out of the post" and return back to the form? Or would it just stop altogether. More of a pre-check, im not sure this could be used as a pre-check or only after a post goes through. – k9b May 22 '14 at 19:13
  • What you could do is have this on the page the form is on so that it returns to the form and displays a message if it's invalid. – TheJokersThief May 23 '14 at 10:43