Posts Tagged regular expression

String case handler :: reg-exp to check whether the string is upcase or not

# #This function basically checks with a regular expression whether the passed value matches with the regular expression or not ,
# #if it does then in that case it will return match data and we wont do a titleize else we will go for titleizing the data , as i am,
# #considering that the data which is a string can be of 3 formats (1/ all upcase 2/ all lowercase 3/ mixed-case), probably this wil handle all
# # the three cases .

def string_case_handler(str_value)
if (/^[A-Z]*$/.match(str_value) == nil)
#if the string doesnt matches then we will titleize it
return str_value.capitalize
else
#if we get matchdata object ,ie it gets matches , no need to do anything just print it
return str_value
end
end

Also if we want to capitalize only the first letter then replace this

str_concat = str_value.first.capitalize + str_value.slice(str_value.first.capitalize.length ,str_value.length)
return str_concat

Add comment February 14, 2008

“Using Regular Expression in Ruby on Rails — Regexp for Password Validation “

A regular expression (abbreviated as regexp or regex,
with plural forms regexps, regexes, or regexen) is a string that
describes or matches a set of strings, according to certain syntax
rules. Regular expressions are used by many text editors and utilities
to search and manipulate bodies of text based on certain patterns. Many
programming languages support regular expressions for string
manipulation. Ruby has a strong Regular Expression engine built
directly as a class of Ruby Programming language as Regexp
Here we will go through an example which will validate the password string.
Lets say we have to implement the following validations to validate a password…

  • Password should contain atleast one integer.
  • Password should contain atleast one alphabet(either in downcase or upcase).
  • Password can have special characters from 20 to 7E ascii values.
  • Password should be minimum of 8 and maximum of 40 cahracters long.

To fulfill above requirements we can have a regular expression like…

/^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

in ruby programming language we can have a number of ways to define this regular expression as…
■ reg = Regexp.new(”^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$”)
or
■ reg = %r(^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$)
or simply
■ reg = /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

Now look what exactly this regex is doing…
(?=.*\d) shows that the string should contain atleast one integer.
(?=.*([a-z]|[A-Z])) shows that the string should contain atleast one alphabet either from downcase or upcase.
([\x20-\x7E]) shows that string can have special characters of ascii values 20 to 7E.
{8,40} shows that string should be minimum of 8 to maximum of 40 cahracters long.
We can simply use this regular expression for manual handling of password in an action as…

def validate_password(password)

  reg = /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

return (reg.match(password))? true : false

end

How to implement this regular expression in a model class in ruby on rails for password validation ?

To implement this regular expression in the model class in the rails way we can do it like…

class MyModel

  validates_format_of :password, :with => /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

end

Have Fun

Powered by ScribeFire.

Add comment December 19, 2007


Recent Posts

Tags

Active Record australian gp calendar date select capistrano capitalize debugging Debugging in rails edge rails formula 1 For those who dont want to update the prototype.js fun Fun baby fun google html in join javascript linus vs windows compatibility linux logging love song malaysian gp 2008 results melbourne migrations music mysql plugin poetry rails regexp regular expression results formula 1 opening race romantic poetry ror Ruby ruby on rails safe html sms song SQL string case handler for strings syantax syntax error for migrations on linux Text Area they can checkout 1.8.3 of the calender date select

Blogroll

Pages