Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Validate serial number
Old 05-13-2006, 11:33 AM Validate serial number
NevadaSam's Avatar
Novice Talker

Posts: 6
Trades: 0
Validate serial number

I am trying to validate a serial number. The serial must be 7 characters: three numbers, followed by two letters, followed by two numbers (Example: 123AB67)
I am able to inter invalid serial such as:999A9A9, 9999AA9, and 999AAA9.

Code:
$serial = uc($serial);
    if (length($serial) > 7
        or substr($serial, 0, 3) !~ m/[0-9]/
        or substr($serial, 3, 2) !~ m/[A-Z]/
        or substr($serial, 5, 2) !~ m/[0-9]/) {
                  # tell user serial is not valid }
    else    {# acknowledge input}
I appreciate someone looking at my code.

thanks,
NevadaSam
NevadaSam is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-13-2006, 12:32 PM Re: Validate serial number
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
one regex is all you should need, the pattern would be

[0-9]{3}[A-Z]{2}[0-9]{2}


(this should be PHP forum though
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-13-2006, 02:51 PM Re: Validate serial number
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Here's a function you can use to check if it's valid or not:

PHP Code:
<?php
function validate_serial($serial)
{
  
$valid false;
  
$pattern '/^[0-9]{3}[A-Z]{2}[0-9]{2}$/';
  
$serial strtoupper($serial);
  
  if(
strlen($serial) == 7)
    
$valid = (preg_match($pattern$serial)) ? true false;

  return 
$valid;
}
?>
This function returns true or false, depending on whether the serial is valid or not. To use it you would do:

PHP Code:
if(validate_serial($s1))
  echo 
'valid serial';
else
  echo 
'invalid serial'
Where $s1 is the serial you want to check.

Cheers,


MC
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 05-13-2006, 06:00 PM Re: Validate serial number
NevadaSam's Avatar
Novice Talker

Posts: 6
Trades: 0
Quote:
Originally Posted by chrishirst
(this should be PHP forum though)
Thanks for responding to my post. I am doing this in Perl which is what I am learning now. If you wish for me to put my future questions abot Perl in the PHP forum, let me know.

Problem Solved!
Thanks,
Samantha

Last edited by NevadaSam; 05-13-2006 at 06:13 PM..
NevadaSam is offline
Reply With Quote
View Public Profile
 
Old 05-13-2006, 06:28 PM Re: Validate serial number
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Aha!

No Perl is fine here, just please mention what language you are using.

Unfortunately I treat Perl a bit like Latin, A long dead language only used by scholars and academics
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-13-2006, 06:45 PM Re: Validate serial number
NevadaSam's Avatar
Novice Talker

Posts: 6
Trades: 0
I was thinking that I should mention "Perl" since this forum in only titled "Coding Forum". I will from now own.

I am hearing that Perl is a dieing langauage, but I have to learn a little about it. I just bought a "PHP & MySQL" book so I will be in the other forum also real soon.
NevadaSam is offline
Reply With Quote
View Public Profile
 
Old 05-13-2006, 07:40 PM Re: Validate serial number
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
OK well for PERL (slowly forgetting this language) the subroutine would be

Code:
sub validate_serial {
  my ($serial) = @_;
  my $valid = false;
  if(length($serial) eq 7) {
    if($serial =~ /^[0-9]{3}[A-Z]{2}[0-9]{2}$/)
    {
      $valid = true;
    }
  }
return $valid;
}
Cheers,


MC
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Validate serial number
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24708 seconds with 12 queries