SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating form fields

    I'm trying to validate VAT Codes entered into a form.

    Whats the best way of checking the contents of a form input? For example, Austria's VAT code has the standard template of "U12345678" (a "U" followed by 8 numbers) and I want to check that the first character of the input value is "U"

    Has anyone got any ideas?

    My current Javascript is:

    Code:
     var co = document.frmPayment.country.value;
     var vatcode = document.frmPayment.VATCode.value;
     var vatfirst = left(vatcode, 1);
     var vatlen = vatcode.length;
     
     alert("Country = '" + co + "'\nVAT Code = '" + vatcode + "'\nVAT Length = '"+ vatlen + "'\nFirst Character = " + firstchar + "'");
     
     if (co=='040' || co=='056' || co=='620' || co=='208' || co=='246' || co=='442' || co=='250' || co=='276' || co=='300' || co=='372' || co=='380' || co=='528' || co=='724' || co=='752') {
     // if any of the VAT code countries are selected
     
     
      if (vatlen==0) {
       alert("Please enter your VAT code");
       document.frmPayment.VATCode.focus();
       return false;
       
      } else {
       if (co == '040') {  // if the country is austria
    	
    	if (vatfirst != 'U') {
    	 alert ("Please enter a valid VAT code.\n\n Eg. U12345678");
    	 document.frmPayment.VATCode.focus();
    	 return false;
    	} 
       }
      }
    
     }
    BTW, the country selection works fine and the length is detected fine.

    Cheers,
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    If everything else works, then this should help you test for a properly formatted VAT code:
    Code:
    var vatcode = document.frmPayment.VATCode.value;
    //create regular expression that matches VAT format
    var RE = new RegExp('(u|U)[0-9]{8}');
    //test regular expression
    var myTest = RE.test(vatcode);
    If (!myTest) {
    //VAT doesn't match format
      alert('Your VAT code does not follow the proper format.');
    }
    //get out of here.
    return myTest;

  3. #3
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vinnie mate - you've saved me a long hard afternoon! Never realised JavaScript had a regular expression module built in... even if I had, I wouldn't have know how to use it!!!

    Cheers very much!

    BTW, have you got any URLs to documentation on how to use the RegExp thing more (eg. all the properties for setting the reg exp?)

    Nice one brudda!
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    http://www.webreference.com/js/column5/ for regular expressions in Javascript specifically, and http://www.regexlib.com/CheatSheet.htm is a good "cheat sheet" for Perl-compatible regular expression syntax (Perl-compatible is the type of regexps used in nearly every language, like JS, Java, .NET, PHP, etc.)

  5. #5
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet - thanks mate!
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    No problem!

  7. #7
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I'm going to be a really cheeky bugger and ask for some help on the templates...

    What would the following convert to as regexp strings?

    (X stands for any alpha character and there are several versions available for some countries)

    France
    12345678901
    X1234567890
    1X123456789
    XX123456789

    (11 characters, alpha either 1st, 2nd, both or neither)

    Ireland
    1234567X
    1X23456X

    (8 characters, alpha either 8th character or 2nd and 8th)

    Spain
    X12345678
    12345678X
    X1234567X

    (9 characters, alpha either 1st, 9th or both characters)



    Thanks for any help on this - really could do with getting it fixed this afternoon!
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  8. #8
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries - I've sorted it now (multiple regexp instances with "and" conditions).

    If anyone else is interested, I'll post the solutions up later (bit busy at the mo).
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •