SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member Opally's Avatar
    Join Date
    Jun 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simplify validationSet from script in DHTML Utopia

    Hi. I've been using the event listener form validation script from DHTML Utopia, it works really well.

    However, because I use some long forms with many similar fields, I end up with a validation set that can look like this (only a snippet here!):

    Code:
    var validationSet = {
      'totstu': {
    	  'regexp': /^[0-9]+$/,
    	  'error': 'Please use whole numbers only'
      },
      'tottea': {
    	  'regexp': /^[0-9]+$/,
    	  'error': 'Please use whole numbers only'
      },
      'totclassesseq': {
    	  'regexp': /^[0-9]+$/,
    	  'error': 'Please use whole numbers only'
      },
      'pct0': {
    	  'regexp': /^((100)|(\d{1,2}))$/,
    	  'error': 'A percentage, please'
      },
      'pct1': {
    	  'regexp': /^((100)|(\d{1,2}))$/,
    	  'error': 'A percentage, please'
      },
      'pct2': {
    	  'regexp': /^((100)|(\d{1,2}))$/,
    	  'error': 'A percentage, please'
      }
    };
    I understand that this is a hash, or associative array. Is there a way to combine these, to simplify and shorten these long lists, in a Javascript set?

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe make a reference array to a default list of validation sets, somethin like
    Code JavaScript:
    var validationDefinitions = {
      'wholenumber': {
    	  'regexp': /^[0-9]+$/,
    	  'error': 'Please use whole numbers only'
      },
      'percentage': {
    	  'regexp': /^((100)|(\d{1,2}))$/,
    	  'error': 'A percentage, please'
      }
    };
     
    var validationSet = {
      'totstu': validationDefinitions.wholenumber,
      'tottea': validationDefinitions.wholenumber,
      'totclassesseq': validationDefinitions.wholenumber,
      'pct0': validationDefinitions.percentage,
      'pct1': validationDefinitions.percentage,
      'pct2': validationDefinitions.percentage
    };

    i dunno if thats correct syntax, but you get the idea

  3. #3
    SitePoint Member Opally's Avatar
    Join Date
    Jun 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, gimbles!

    I actually got a similar reply from another JS list at codingforums, but I thank you very much for your assistance!!! I love finding better ways to do things. The difference in your reply is interesting. You can see the codingforums thread here

    I wish I could find a Perl message board that was as helpful as the Javascript message boards.


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
  •