Results 1 to 4 of 4

Thread: Strings in javascript

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    Strings in javascript

    Hi,

    I need some examples of string functions in javascript. My aim is to validate a date value to check that it is in the format that I want it to be in. I will need to an equivilent of vb split function or instr and mid.

    Anyone got some examples for me?

  2. #2
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    there is a javascript split function too. Theres also substring and indexOf that maybe what you require.

    Have a read of this. It shows all methods and functions associated with the js string object

    http://www.w3schools.com/js/js_string.asp
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  3. #3
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    You can also check my sig for links to the documentation on JavaScript, where you can find everything you need to know about the String object.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    You can use regular expressions for patter matching

    eg.

    Code:
    //lets check for dd/mm/yyyy
    
    correct_date = "02/03/2002";
    incorrect_date = "02/03/02";
    
    if( correct_date.match(/^\d{2}\/\d{2}\/\d{4}$/))
    {
      alert("date matches");
    }
    
    if( incorrect_date.match(/^\d{2}\/\d{2}\/\d{4}$/))
    {
      alert("date matches");
    }
    the previous example uses this regular expression.

    ^\d{2}\/\d{2}\/\d{4}$

    and the regular expression is enclosed by /regex/

    ^ = match the begiing of the string
    \d = any numeric char
    {n} = match n times the previous element
    \/ = a literal /
    $ = match the end of the string

Posting Permissions

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



Click Here to Expand Forum to Full Width