https://stackoverflow.com/questions/619977/regular-expression-patterns-for-tracking-numbers
Will you know in advance which service you'd be validating against? (UPS/FedEx/etc) Or will you need to detect it by the given tracking number?
Hi, i need to validate the user input (tracking number) of USPS, UPS, FedEx
i believe someone already have the regular expression for this.
anybody please ??
https://stackoverflow.com/questions/619977/regular-expression-patterns-for-tracking-numbers
Will you know in advance which service you'd be validating against? (UPS/FedEx/etc) Or will you need to detect it by the given tracking number?
Alternatively you can use each of those companies apis. That gives the added bonus that not only the format is valid, but that it actually is registered/exists in their system, which may be more important than just if the format is valid, depending on your use case.
Thanks @Cronix
Yes, i have a carrier selector (dropdown) before entering the tracking number
i tried several solutions it seems to me USPS & UPS are working fine.
but i stuck on a FedEx Tracking Code. for example i have this FedEx Tracking Code "4628946685" if you go to fedEx website and try to track it .. you can see it is a valid tracking code
but your given StackOverflow answer and whatever i have found so far .. doesn't let fedEx to enter 10 Digits. so i think none of this regex well updated.
thanks
and yes i'm trying with USPS API,
actually our system need to show the shipment-status and mark them as delivered automatically. so as we are doing now is - every 30 minutes we check usps api whether it is delivered or not. at this point i'm also interested to know .. if there has anything like webhook .... so that whenever a package delivered usps will hit one of our server-url and we don't need to check for delivery status every 30 minutes... just curious maybe there has something which we don't know yet.
btw you are very helpful :)
Yes, also fedex (and others) have multiple formats, so you'd need to check against all of them for each one. If you use the api's you will never have a problem as those would always be kept up to date. If you use regex, you'll have to manually update them as the carriers add more formats.
So for FedEx, it shows 3 separate formats
/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/
/\b((98\d\d\d\d\d?\d\d\d\d|98\d\d) ?\d\d\d\d ?\d\d\d\d( ?\d\d\d)?)\b/
/^[0-9]{15}$/
If you want to validate 10 digits, then you'd just create an additional one that is identical to the last one listed there, except have {10} for 10 digits instead of {15}.
@fidahasan08 - not sure if this is helpful but this item appeared on laravel news a while ago
https://laravel-news.com/shippo-multi-carrier-shipping-api-laravel-apps
Might save you reinventing the wheel and their php library does tracking for the couriers you need
Yes i have added the 10 digits expression for FedEx but not happy with it yet, coz its not bullet-proof solution :(
you are right, we need to go with API... not only for delivery-status check also for tracking-code validation.
I don't think they have a callback (they might) but I know they allow you to set up an email alert for when packages are delivered (which can be setup via the api too). If they don't have any other way via the api to alert you upon delivery, you could use the email system, although it's a bit tricky. I've done similar things in the past.
At least then you wouldn't have to be polling them every x minutes to see if something was delivered and would be pretty close to "real time"
@Cronix thanks for the suggestion,
a lot of task to parse the email etc... (i'm lazy enough hahaha )
but will definitely give it a try later.
@D9705996 thanks for your response :)
i just checked, that is not free. we are not going to any paid service for now.
@fidahasan08 - no worries. Not sure how the pricing model worked as it seemed like 5 cents per shipment. Not sure if you could use the tracking functionality without actually shipping with shippo.
The possible courier codes are [:usps, :fedex, :ups, :ontrac, :dhl, :amazon, :s10, :unknown]. S10 is the international standard used by local government post offices. When packages are shipped internationally via normal post, it's usually an S10 number.
# => #<TrackingNumber::UPS 1Z879E930346834440>
t.valid? #=> true
t.courier_code #=> :ups
t.courier_name #=> "UPS"
t = TrackingNumber.new("RB123456785GB")
t.courier_name #=> "Royal Mail Group plc"
t.courier_code #=> :s10
t = TrackingNumber.new("RB123456785US")
t.courier_name #=> "United States Postal Service"
Service Type
Some tracking numbers indicate their service type
t = TrackingNumber.new("1Z879E930346834440")
t.service_type #=> "UPS United States Ground""
t = TrackingNumber.new("1ZXX3150YW44070023")
t.service_type #=> "UPS SurePost - Delivered by the USPS"
t = TrackingNumber.new("RB123456785US")
t.service_type #=> "Letter Post Registered"
Shipper ID
Some tracking numbers indicate information about their package
t = TrackingNumber.new("1Z6072AF0320751583")
t.shipper_id #=> "6072AF" <-- this is Target
Destination Zip
Some tracking numbers indicate their destination
t = TrackingNumber.new("1001901781990001000300617767839437")
t.destination_zip #=> "10003"
ActiveModel validation
For Rails 3 (or any ActiveModel client), also use liteblue to get all benifits. validate your fields as a tracking number:
class Shipment < ActiveRecord::Base
validates :tracking, :tracking_number => true
end
Sometimes it's helpful to have a "magic" tracking number that isn't valid for any of the real carriers, and maybe will have other side effects (e.g., special treatment of a shipping email.)
class Shipment < ActiveRecord::Base
validates :tracking, :tracking_number => { :except => 'magic-hand-delivery' }
end
Please or to participate in this conversation.