Skip to content

Instantly share code, notes, and snippets.

@zoras
Forked from bikashp/gist:589180
Created October 4, 2010 08:42
Show Gist options
  • Save zoras/609395 to your computer and use it in GitHub Desktop.
Save zoras/609395 to your computer and use it in GitHub Desktop.
def valid(format, value)
case format
when "email"
regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
when "url"
regex = /^(http:\/\/|https:\/\/)?(www.)?[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3})([.]?[a-zA-Z]{2})?(\/\S*)?$/i
msg = "Start with http:// or https://"
when "number"
regex = /^\d{1,3}(\,?\d{3})*$/
when "date"
# Check if date (dd.mm.yyyy) is valid date or not
#regex = /\d{1,2}(\/|-|\.|\s)\d{1,2}(\/|-|\.|\s)\d{2,4}/
test = check_date(value)
return test
when "time"
regex = /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/
when "datetme"
regex = /^(1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2}|(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/
when "currency" #=> for USD
regex = /^\$?(?:\d+|\d{1,3}(?:,\d{3})*)(?:\.\d{1,2}){0,1}$/
when "general"
return true
else
puts "Invalid format:: #{format}"
return false
end
if !value.nil?
value.match(regex).nil? ? false : true
else
return true
end
end
def check_date(value)
begin
val = value.scan(/^(\d{1,2})[\/|-|\.|\s](\d{1,2})[\/|-|\.|\s](\d{2,4})$/) # dd.mm.yyyy
Date.civil(val[0][2].to_i,val[0][1].to_i,val[0][0].to_i) # civil check julian date in yyyy.mm.dd
!val.blank? ? true : false
rescue
return false
end
end
@zoras
Copy link
Author

zoras commented Oct 4, 2010

when "url"

    regex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix

    test match scenario: 
    http://www.en.hotmail-f.com.np
    https://www.apple.co.uk
    http://www.apple.com
    http://www.apple.com
    http://w3.apple.tv
    http://www.apple-apple.com
    http://www.apple3.com.np
    http://en.apple.com
    http://www.apple.co.uk
    http://aaa.apple.co.uk
    https://www.apple.com.uk
    https://w3.apple.tv
    https://www.apple-apple.com
    https://www.apple3.com
    https://en.apple.com
    https://www.apple.com

when "currency" => for USD

    regex = /^\$?(?:\d+|\d{1,3}(?:,\d{3})*)(?:\.\d{1,2}){0,1}$/

    test match scenario:(passed)
    $1,113,000.00
    $3.99
    $5,000
    $1.0
    $22,222,222,222,222,222
    $74387498372947387483978934758744329.00

when "number"

    regex = /^\d{1,3}(\,?\d{3})*$/

    test match scenario:(passed)
    2,468,273,643,872
    11111111111111

when "email"

    regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

    test match scenario:(passed)
    dlamichhane@hotmail.com
    a@a.com
    a123@123.com
    dlamichhane@hotmail.co.uk

when "date"

    regex = /^(1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2}$/

    test match scenario:(passed)
    Match m/d/yy and mm/dd/yyyy
    12/03/2010 and 09/4/2010

when "time"

    regex = /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/

    test match scenario:(passsed)
    hh:mm:ss
    23:59:59
    00:00:00
    01:1:1
    12:12:12

when "datetme"

    regex = /^(1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2}|(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/

    test match scenario:(passed)
    mm/dd/yyyy+hh:mm:ss
    12/31/200312:12:12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment