Check for special characters

I want to check the login with special characters !@#S%^*()+_-?/<>:"';. space using regular expression in ruby ​​on rails. These special characters should not be acceptable. What is the code for this?

+3
source share
6 answers

validates_format_of :username, :with => /^[A-Za-z0-9.&]*\z/

will work

+7
source

You have received regular expressions in this thread that answer your specific question. You do a blacklisted approach (blocking characters you don't need), but is it really better? I see that you did not cover and / or ~, and there are many other special characters that are probably still missing.

, " " pablorc regexp. , ... : , .

, .

def valid_login?(str)
    return true if (/^\w*$/.match(str))
    return false
end

valid_login?, true, , , ( , , ) .

:

> valid_login?("testy") 
  true
> valid_login?("a b")
  false
> valid_login?("a'")
  false
+3

, , , , :

^[^!@#\$%\^\*\(\)\+_\-\?/\<\>:"';\. ]$
+1

/^\w*$/ , .

, cheatsheet live ruby ​​regexp editor http://rubular.com

+1

-, , authlogic.

. ..

.

0
validates_format_of :username, :with => /^[^!@#S%\^\*()\+_-\?\/<>:\"';]+$/
0

Source: https://habr.com/ru/post/1747554/


All Articles