Two Basic Questions about Prompt Boxes

Discussion in 'JavaScript' started by BigMumu, Sep 21, 2007.

  1. #1
    Hi!

    I just started learning Javascript & while practicing I got two problems I couldn't resolve.

    The 1st is about this code:
    if (name!=null && name!="")
     {
     document.write("whatever")
     }
    Code (markup):
    How can I add to it different actions for different input values?

    Can I for example change
    name!=null && name!=""
    Code (markup):
    to
    & add a code for doing nothing between { } ? I tried to leave it blank but it didn't work :eek:

    If I could do that I would be able then to add if ... else for different input values entered in the prompt box.

    ---------------

    My second question is about the input values, in my tests i found they are case sensitive. Is there a way to remove that? I mean I want when users type in the prompt box digitalpoint, DigtalPoint, dIgitAlpOinT, etc they get the same action executed.

    I tried my best to explain, I hope you got my point ... thanks in advance! :)
     
    BigMumu, Sep 21, 2007 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    JavaScript is a case sensitive language; there's no way to change that. But here's what I do if I want to effect case-insensitive validation, where inputString is the string coming from the prompt box:

    
    if ( inputString.toLowerCase() == "digitalpoint" )
      { do stuff }
    Code (markup):
    The trick here is the toLowerCase method - check it out at w3schools.com/jsref/jsref_toLowerCase.asp.
     
    KatieK, Sep 21, 2007 IP
    BigMumu likes this.
  3. BigMumu

    BigMumu Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nice! One problem solved! Thanks buddy!! I added to your reputation :)

    Any help would be appreciated for my other problem, thanks! :)
     
    BigMumu, Sep 21, 2007 IP
  4. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #4
    No problem, and thanks for the props.

    I was thinking about your first question, and there are a couple of things to know that might help you out.

    First off, && indicates AND. So

    if ( color == "blue" && color == "red" )
    Code (markup):
    will never evaluate to true, because color can't == both "blue" and "red". But

    if ( color != "blue" && color != "red" )
    Code (markup):
    will sometimes be true, for instance if color == "green".

    Second, when I want to 'do nothing', I just put some comments inside the { }:

    if ( color != "blue" && color != "red" )
    { /*  Do nothing  */ }
    else
    { alert("Could be green!"); }
    Code (markup):
    I apologize in advance if any of my syntax is wrong - I was just typing into the comments box here.

    :)
     
    KatieK, Sep 21, 2007 IP
  5. BigMumu

    BigMumu Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks! What I did is I changed the 'and' with 'or' & it worked! So instead of:
    if (name==null [COLOR="Red"]&&[/COLOR] name=="")
     {
    
     }
    else if
    Code (markup):
    I put:
    if (name==null [COLOR="Red"]||[/COLOR] name=="")
     {
    
     }
    else if
    Code (markup):
    No it's just working fine, thanks again for help :)
     
    BigMumu, Sep 21, 2007 IP
  6. BigMumu

    BigMumu Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    BigMumu, Sep 21, 2007 IP
  7. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Not too shabby. :)

    When I was first learning JavaScript, I made good traction by using the JavaScript tutorials on HTML Goodies. (htmlgoodies.com/primers/jsp/article.php/3478531)

    The material's not modernized (it doesn't talk about Ajax or unobtrusive JS), but it does give you a good idea of what JavaScript can and can't do.
     
    KatieK, Sep 22, 2007 IP
  8. BigMumu

    BigMumu Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    BigMumu, Sep 22, 2007 IP