SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    storing certain form value in var

    Hello

    lets say my form value is

    &hello john &bye jacob &goodafternoon

    How can I store all the values starting with & in a var?

    Regards

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Jaynesh View Post
    How can I store all the values starting with & in a var?
    You can use a regular expression that gets all of the characters that start with an ampersand.

    Code javascript:
    var matches = field.value.match(/(&\S+)/g);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thank you for your response.

    Yes but how do I store that in a var.

    for example If the form value is &hello jacob
    How do I store &hello in a var?

    Regards

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Jaynesh View Post
    Yes but how do I store that in a var.
    The example that I gave stores the values in a var called matches as an array, such as:
    ['&hello', '&bye', '&goodafternoon']

    If you don't want the ampersand included in the string, you can match /&(\S+)/g instead of /(&\S+)/g

    You can use the standard array-index notation to retrieve one of the string values from that array.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •