全名正则表达式验证

时间:2014-06-07 08:20:56

标签: ruby regex validation

我的验证员有以下测试用例:

context 'Validation successful' do
  specify :full_name do
    expect(validator.(:full_name, 'Full Name'                          ))
    expect(validator.(:full_name, 'Ka El'                              ))
    expect(validator.(:full_name, 'José Manuel Barroso'                ))
    expect(validator.(:full_name, 'Janusz Korwin-Mikke'                ))
    expect(validator.(:full_name, 'Jean-Claude Van Damme'              ))
    expect(validator.(:full_name, 'Ms. Jan Levinson-Gould'             ))
    expect(validator.(:full_name, 'Robert Downey, Jr.'                 ))
    expect(validator.(:full_name, "Joanna d'Arc"                       ))
    expect(validator.(:full_name, 'Dr. Martin Luther King, Jr.'        ))
    expect(validator.(:full_name, "Brett d'Arras-d'Haudracey"          ))
    expect(validator.(:full_name, 'prof. dr hab. n. med. Szymon Dragan'))
  end
end

context 'Validation failure' do
  specify :full_name do
    expect { validator.(:full_name, 'Full  Name') }.to raise_error
    expect { validator.(:full_name, 'Full N4m3' ) }.to raise_error
    expect { validator.(:full_name, 'Full N@me' ) }.to raise_error
    expect { validator.(:full_name, 'FULL NAME' ) }.to raise_error
    expect { validator.(:full_name, 'FuLl NaMe' ) }.to raise_error
    expect { validator.(:full_name, 'Full NamE' ) }.to raise_error
    expect { validator.(:full_name, 'K L'       ) }.to raise_error
    expect { validator.(:full_name, 'full name' ) }.to raise_error
    expect { validator.(:full_name, 'Full name' ) }.to raise_error
    expect { validator.(:full_name, 'full Name' ) }.to raise_error
    expect { validator.(:full_name, 'FullName'  ) }.to raise_error
    expect { validator.(:full_name, ''          ) }.to raise_error
  end
end

我找不到任何能满足我的全名的正则表达式。我还试图通过特殊字符拆分名称然后验证每个元素,但这导致我不验证组之间的空格等等。

def name_valid?(name)
  name.match /\A[^0-9~`!@#$%\^&*+_={}?()\[\];:<>]+\Z/
end

如何处理全名验证?

0 个答案:

没有答案