web开发中,虽然 Javascript 不是你唯一的form验证方式,但使用它肯定会对站点的可用性和效率有帮助。
Magento 中的 Javascript 验证
默认情况下 Magento 使用form.js (js/varien/form.js) 来提供抽象形式的 Javascript 函数,我认为-此class的最有用的应用就是表单验证。form.js 的验证类是Prototype库的一部分。它通过检查input中class的名称的形式来工作,即验证器通过class名称对用户的输入进行有效性验证。
Custom Form Validation
添加 Javascript 验证自己的表单是非常简单的。
首先,您需要创建一个form对象 (form.js)
第一个参数,是表单的ID。
注意看required-entry和validate-email,它告诉验证规则这个input必须不能为空和必须是正确的email格式地址,否则将不予通过。
Please select an option
This is a required field
Please enter a valid number in this field
Please use numbers only in this field. please avoid spaces or other characters such as dots or commas
Please use letters only (a-z or A-Z) in this field.
Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.
Please use only letters (a-z or A-Z) or numbers (0-9) only in this field. No spaces or other characters are allowed
Please use only letters (a-z or A-Z) or numbers (0-9) or spaces and # only in this field
Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890
Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890
Please enter a valid fax number. For example (123) 456-7890 or 123-456-7890
Please enter a valid date
Please enter a valid email address. For example [email protected].
Please use only letters (a-z or A-Z), numbers (0-9) , underscore(_) or spaces in this field.
Please enter 6 or more characters. Leading or trailing spaces will be ignored
Please enter 7 or more characters. Password should contain both numeric and alphabetic characters
Please make sure your passwords match
Please enter a valid URL. http:// is required
Please enter a valid URL. For example http://www.example.com or www.example.com
Please enter a valid Identifier. For example example-page, example-page.html or anotherlevel/example-page
Please enter a valid XML-identifier. For example something_1, block5, id-4
Please enter a valid social security number. For example 123-45-6789
Please enter a valid zip code. For example 90602 or 90602-1234
Please enter a valid zip code
Please use this date format: dd/mm/yyyy. For example 17/03/2006 for the 17th of March, 2006
Please enter a valid $ amount. For example $100.00
Please select one of the above options.
Please select one of the options.
Please enter a valid number in this field
Please select State/Province
Please enter 6 or more characters. Leading or trailing spaces will be ignored
Please enter a number greater than 0 in this field
Please enter a number 0 or greater in this field
Please enter a valid credit card number.
Credit card number doesn't match credit card type
Card type doesn't match credit card number
Incorrect credit card expiration date
Please enter a valid credit card verification number.
Please use only letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field, first character should be a letter.
Please input a valid CSS-length. For example 100px or 77pt or 20em or .5ex or 50%
Maximum length exceeded
英文原文出处http://fishpig.co.uk/blog/magento-forms-prototype-javascript-validation.html