23
Apr

Ok, so you have a nice custom module with a form – now you want to add basic javascript validation to it. Simple. Example,

<input type="text" name="first_name" value="" size="20" class="required-entry input-text" />

The class required-entry will make the field mandatory – this works for text-box and drop down.

If your form appears on the “one page checkout” page then, this is all you need to do.

If your form appears on its own then you will need to add this code at the bottom of your page:

<script type="text/javascript">
var customForm = new VarienForm('my-form-id');
</script>

Ensure that your form name and id are the same and that is what you need to pass to VarienForm

,

gayatri.sa
Posted by on 23 Apr 2010 by gayatri.sa in Magento eCommerce Tips

One Response to “Add javascript validation to your Magento forms”

  • merenpoort

    Hallo gayatri.se

    I have a problem with custom Validation, and hope you like to help me.

    I have a product (magento shop) with following size:

    Width: from 0.9m to 2.20m

    Height: from 1m to 3m

    But the biggest size allowed is less than 4 square meters.

    and i like to validate the imput fields:

    Form

    Width:

    Hight:

    function formCallback(result, form) {

    window.status = “valiation callback for form ‘” + form.id + “‘: result = ” + result;

    }

    var valid = new Validation(‘test’, {immediate : true, onFormValidate : formCallback});

    Validation.addAllThese([

    ['validate-width', 'allowed width muss be behind 900mm and 22000 mm ', {

    min : 900,

    max : 22000

    }],

    ['validate-hight', 'allowed hight muss be behind 1000mm and 30000 mm', {

    min : 900,

    max : 22000

    }],

    ['validate-size', 'allowed is max size of 4 square meter', function(v) {

    return ((document.test.cs-width.value * v)>400000000)

    }]

    ]);

    what i do wrong?

    I will be happy if you answer my E-Mail

    Dariusz

Add reply

*