1

I am trying to allow Blank inputs on my form but also validate an email if ever the user inputs one, i already changed the regex several times with the ones that i find here in stackoverflow that allows blank input but all of them doesn't work

here is the original code:

['validate-email', {
        errorMsg: Form.Validator.getMsg.pass('email'),
        test: function(element){
            return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+\/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
        }
    }],

how can i allow my mootools form validator to accept blanks but also verify email if there is any input?

Direct Source: http://mootools.net/docs/more/Forms/Form.Validator

Ry-
  • 218,210
  • 55
  • 464
  • 476
telexper
  • 2,381
  • 8
  • 37
  • 66

2 Answers2

1

I see two options.

Option 1

Remove the required class from the input element. That will accept an empty value but check/validate if not empty. Try it here.

Option 2

The one you already have :)

I normally use this regex:

/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i

Found also this one. Anyway, it works; check this demo.

Community
  • 1
  • 1
Sergio
  • 28,539
  • 11
  • 85
  • 132
0

Email validation regex is so clumsy... Does mootools support validation functions? Anyway, you can take your regex and create a bit more clumsy one: original-regex|^$, which will accept empty string

kirilloid
  • 14,011
  • 6
  • 38
  • 52