Question

I'm adapting a small Zend e-commerce website and lately we have received some robot inputs who just keep filling in email adresses in every field. This is why I want to have my form validation check that the field is NOT an email(or maybe just NOT contains a "@") to pass. Other idea would be to have a hidden field that can not be filled in. How do I achieve this? As I find it hard to find a way to make the result be a false in order to pass (not email).

There are two validations currently used, one of them is jQuery Validation plugin

setupFormValidation : function() {
            $('#planOrderForm').validate({
                ignore: [],
                rules : {
                    contactPerson:{
                        required: true,
                        minlength: 4
                    },
                    contactEmail: {
                        required:true,
                        email: true
                    },
                    contactPhone: 'required',
                    contactAddress: 'required',
                    contactZipcode: 'required',
                    contactCity: 'required'
                },
                messages : {
                    contactPerson:  {
                        required: $('#name-message-1').html(),
                        minlength: $('#name-message-2').html()
                    },
                    contactEmail: { 
                        required: $('#email-message-1').html(),
                        email: $('#email-message-2').html()
                    },
                    contactPhone: $('#phone-message').html(),
                    contactAddress: $('#address-message').html(),
                    contactZipcode: $('#zip-city-message').html(),
                    contactCity: $('#zip-city-message').html()
                },
                errorElement : 'div',
                errorPlacement: function(error, element) {
                    if( element.attr('id')=='contactZipcode'||element.attr('id')=='contactCity'){
                        console.log(element.parent());
                        if(element.parent().find('.error-message').length==0){
                            error.insertAfter('#contactCity');
                            error.addClass('error-message');
                        }
                    } else {
                        offset = element.offset();
                        error.insertAfter(element);
                        error.addClass('error-message');
                    }
                },
                submitHandler : function(form) {
                    form.submit();
                }
            });

The other one is through the Zend addElement function:

/* email */
$this->addElement('text', 'contactEmail', array(
'label' => 'form_plan_email',
'required' => true,
'trim' => true,
'validators' => array('EmailAddress'),
'maxLength' => 255,
'decorators' => $customElementDecorators
));

The preferred method would be through jQuery form validation.

enter image description here

Was it helpful?

Solution

Quote OP:

"The preferred method would be through jQuery form validation."

To do this with the jQuery Validation plugin, you would write a custom rule using the .addMethod() method. Return true to pass and false to fail validation (display message).

jQuery.validator.addMethod("noEmail", function(value, element) {
      return (value.indexOf("@") == -1);
}, "Please don't enter an email address");

declared:

rules:  {
    myfield: {
        noEmail: true
    }
}

DEMO: http://jsfiddle.net/CW7Pt/


However, what makes you think the spam-bots are going to be using any jQuery/JavaScript? All anybody has to do is disable the JavaScript in their browser and there is no jQuery Validation. Since the spam-bots emulate browsers, they likely wouldn't bother to have anything enabled (like JavaScript) that would impede their task.

The far better solution is to use server-side (PHP & Zend) validation, which cannot be bypassed or disabled by any user.

framework.zend.com/manual/1.11/en/zend.validate.writing_validators.html

You would still use the jQuery Validation plugin to achieve a better user experience for your honest users.

OTHER TIPS

You can check Zend's Email validator, copy it contents and revert it's result. Then just add it to your element:

$notmail = new MyScripts_Validators_NotMail();

$this->addElement('text', 'contactEmail', array(
'label' => 'form_plan_email',
'required' => true,
'trim' => true,
'validators' => array($notmail),
'maxLength' => 255,
'decorators' => $customElementDecorators

You should not worry about JS. This is information for normal people, and those who paste email everywhere are not nromal peopl so you dont have to worry that they will see ugly error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top