A simple text field besides a rectangular box , how to ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redcodeheaven
    New Member
    • Nov 2008
    • 13

    A simple text field besides a rectangular box , how to ?

    Hi guys,I was thinking about the code to :
    write something inside the text box and if the word entered is equal "hero" for example the box next to it will turn green means correct, otherwise it will turn red means wrong,and if nothing entered it stays white.
    I really like to know how we code that in javascript using functions, without any CSS or anything else.
    That means we have 2 objects a text box and a rectangle for feedback color.
    Remark: there is no button at all, the validation will be each time you are typing.


    A Bonus question : if someday we decide to change the color with pictures , is it possible to do that?
    Thank you very much for your help, much appreciate it.
  • Jibran
    New Member
    • Oct 2008
    • 30

    #2
    This is possible. A good example is the password strength custom component which changes color based on the user password length. You can google it.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      without any CSS or anything else.
      why making it overly complicated like that?

      Comment

      • redcodeheaven
        New Member
        • Nov 2008
        • 13

        #4
        ok you can use whatever you feel conveniable

        ok you can use whatever you feel conveniable.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          the approach is quite straightforward : if the user entered something, i.e. changed the input value (onchange), test if the phrase matches the default. in this case swap the class names of the indication field so that you get the right look (different colour or image).

          Comment

          • redcodeheaven
            New Member
            • Nov 2008
            • 13

            #6
            I appreciate if someone can bring up the solution .

            I appreciate if someone can bring up the solution .Thanks.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              you mean, writing the code for you?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                If you want an example, here's one. Not necessarily the best example to learn what's going on because I didn't look for a tutorial and this was one I'd seen years ago. If you search, you'll find many useful tutorials out there.

                Try something and, if you get stuck, post back here with your code.

                Comment

                • redcodeheaven
                  New Member
                  • Nov 2008
                  • 13

                  #9
                  Hi all.

                  I am not trying to push anyone to write the code, but I am trying to learn.How can an object affect the other object.Those replies about some examples written are not really the best example.Is there something more precise if nobody like to write the code?.
                  Thank you.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    just a little explanation from what I said:

                    if the user entered something, i.e. changed the input value (onchange),.
                    here I refer to an event (the event, when a user enters a character in the input field)
                    Code:
                    // [I]input[/I] refers to the text box element
                    // onchange is the event, that occurs if a [I]value[/I] changes
                    // doSomething() is the function executed, when the event occurs
                    // [I]false[/I] means it’s in the bubbling phase
                    
                    [I]input[/I].addEventListener("change", doSomething, false);
                    test if the phrase matches the default.
                    the condition, when something should be done
                    Code:
                    if ("hero" == this.value)
                    {
                        // execute code
                    }
                    in this case swap the class names of the indication field so that you get the right look (different colour or image)
                    styling, such as background colours, background images, etc. should be done using CSS. a very simple way to change the styling is by attaching classes to the elements (see e.g. CSS Zen Garden). if you style the class with CSS, you don’t have to alter the JavaScript, if you need a new styling.
                    Code:
                    // a basic approach
                    // [I]elem[/I] refers to the indication element
                    if ("hero" == this.value)
                    {
                        [I]elem[/I].className = "correct";
                    }
                    else
                    {
                        [I]elem[/I].className = "wrong";
                    }

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      What did you not understand about the example I posted? The approach is the same or similar to what Dormilich has been suggesting.

                      Comment

                      • redcodeheaven
                        New Member
                        • Nov 2008
                        • 13

                        #12
                        Thank you for all the effort you put.

                        acoder and Dormilich thanks both of you for your reply.
                        Dormillich , I really appreciate your help and sorry to take your time.
                        I will try to write the code and see what I got and hopefully learn from it.
                        Thank you Sir.

                        Comment

                        Working...