concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NotGiven

    concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

    I have a field called telephone whose ONBLUR action is to call a javascript
    function:
    validatePhoneNu mber(telephone)

    The non-working function is:

    function validatePhoneNu mber(v)
    {
    var phone =document.form. + v + .value; //HERE"S WHAT IS
    NOT WORKING
    var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
    .........//code left out for clarity
    document.form. + v + .focus()
    }

    I want to reuse the function so I need to make it generic. Any ideas?

    Many thanks.



  • ManoDestra

    #2
    Re: concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

    The first line in your function is not going to work. Pass in the value of
    document.form.v .value directly and work with that.

    If you need to make it generic across a site, you could pass in the form
    element itself which is what I think you are trying to do here. It would
    then become...

    var phone = document.form.v .value; (Assuming that your form is called form
    and that v is one of the named elements within it).

    Personally, I would still go with the first option, pass in the value of the
    text field to the function and let the function validate the string.

    Peter.

    "NotGiven" <noname@nonegiv en.net> wrote in message
    news:GefTa.3227 $nL2.1605@fe05. atl2.webusenet. com...[color=blue]
    > I have a field called telephone whose ONBLUR action is to call a[/color]
    javascript[color=blue]
    > function:
    > validatePhoneNu mber(telephone)
    >
    > The non-working function is:
    >
    > function validatePhoneNu mber(v)
    > {
    > var phone =document.form. + v + .value; //HERE"S WHAT IS
    > NOT WORKING
    > var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
    > .........//code left out for clarity
    > document.form. + v + .focus()
    > }
    >
    > I want to reuse the function so I need to make it generic. Any ideas?
    >
    > Many thanks.
    >
    >
    >[/color]


    Comment

    • Evertjan.

      #3
      Re: concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

      NotGiven wrote on 22 jul 2003 in comp.lang.javas cript:
      [color=blue]
      > I have a field called telephone whose ONBLUR action is to call a
      > javascript function:
      > validatePhoneNu mber(telephone)
      >
      > The non-working function is:
      >
      > function validatePhoneNu mber(v)
      > {
      > var phone =document.form. + v + .value; //HERE"S WHAT
      > IS
      > NOT WORKING
      > var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');
      > .........//code left out for clarity
      > document.form. + v + .focus()
      > }
      >[/color]

      var phone =document.form. + v + .value;

      this will never work, because the + only works in a string

      var phone ="document.form ." + v + ".value";

      This should work,
      because you need a string as you want to strip it in the next line
      var stripped = phone.replace(. ....

      =============== =

      document.form. + v + .focus()

      Read: <http://www.litotes.dem on.co.uk/js_info/sq_brackets.htm l>

      document.form[v].focus()

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Philip Ronan

        #4
        Re: concatenate in Javascript function? simple field validationfunci ton not working - needs to concat field name with object

        On 03.7.22 7:28 PM, NotGiven wrote:
        [color=blue]
        > I have a field called telephone whose ONBLUR action is to call a javascript
        > function:
        > validatePhoneNu mber(telephone)[/color]

        Can I just add that it's rather bad manners to use onblur() events to check
        form fields.

        It's like having someone breathing down your neck the whole time. Put this
        in an onsubmit() handler instead.

        Phil

        --
        Philip Ronan
        phil.ronanzzz@v irgin.net
        (Please remove the "z"s if replying by email)


        Comment

        • Richard Cornford

          #5
          Re: concatenate in Javascript function? simple field validationfunci ton not working - needs to concat field name with object

          "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
          news:BB4347AD.1 6F3E%phil.ronan zzz@virgin.net. ..
          <snip>[color=blue]
          >Can I just add that it's rather bad manners to use onblur()
          >events to check form fields.
          >
          >It's like having someone breathing down your neck the
          >whole time. Put this in an onsubmit() handler instead.[/color]

          I am not sure that validating onBlur itself can be considered bad
          manners, it is the possible alert (or similar warning requiring user
          interaction) and especially the re-focusing of the form field, trapping
          the user into completing that field (if not the entire form) before they
          can do anything else, that is objectionable.

          Consider, for example, a field for entering your height that is
          validated onBlur but all the validation script does is reveal some text
          adjacent to the field that reads "Are you really 70 feet tall?" (or
          something similar). The user is not prevented from getting on with
          whatever they had left the field to do but reviewing the form later they
          will be made aware that their entry for that field seems erroneous (and
          will probably be rejected by the server if submitted).

          Certainly I would prefer to validate a form in its entirety onSubmit as
          it is the mechanism provided for that task (at leas I assume that was
          the idea behind onSubmit).

          Richard.


          Comment

          • Dr John Stockton

            #6
            concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

            JRS: In article <BB4347AD.16F3E %phil.ronanzzz@ virgin.net>, seen in
            news:comp.lang. javascript, Philip Ronan <phil.ronanzzz@ virgin.net>
            posted at Tue, 22 Jul 2003 19:57:33 :-[color=blue]
            >
            >Can I just add that it's rather bad manners to use onblur() events to check
            >form fields.
            >
            >It's like having someone breathing down your neck the whole time. Put this
            >in an onsubmit() handler instead.[/color]

            Forms are not necessarily intended to be Submitted.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

              Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
              [color=blue]
              > Forms are not necessarily intended to be Submitted.[/color]

              Forms must have an action attribute which represents how to submit
              them. I would say that forms *are* intended (by the specification) to
              be submitted, even if they somethimes are not by page authors. Those
              page authors are the ones giving us, e.g., 'action="javasc ript:;"'.

              If you need input elements but don't need them to be submitted, then
              you don't need a form element at all. After all, it has no visible
              effect on the page, and if you don't submit, no semantic effect
              either, so it might as well not be there. The only reason to include
              it is easy access to the input elements (though document.forms[]).

              /L 'Just say no to meaningless forms'
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Dr John Stockton

                #8
                Re: concatenate in Javascript function? simple field validation funciton not working - needs to concat field name with object

                JRS: In article <1xwhgl10.fsf@h otpop.com>, seen in
                news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                posted at Wed, 23 Jul 2003 20:23:07 :-[color=blue]
                >Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
                >[color=green]
                >> Forms are not necessarily intended to be Submitted.[/color]
                >
                >Forms must have an action attribute which represents how to submit
                >them. I would say that forms *are* intended (by the specification) to
                >be submitted, even if they somethimes are not by page authors. Those
                >page authors are the ones giving us, e.g., 'action="javasc ript:;"'.[/color]

                The author creates the actual forms!
                [color=blue]
                >If you need input elements but don't need them to be submitted, then
                >you don't need a form element at all. After all, it has no visible
                >effect on the page, and if you don't submit, no semantic effect
                >either, so it might as well not be there. The only reason to include
                >it is easy access to the input elements (though document.forms[]).[/color]

                There has for me been at least one other reason, although I do not
                recall what it was.

                It may be, as you suggest, to do with element addressability; or it may
                be to do with one or more of the checkers/validators that I have used
                insisting that input elements must be in Forms (or at least raising a
                complaint that I could only resolve that way). It may be something that
                I read here.

                However, I can remove the form-enclosure from my js-alarm.htm (apart
                from only having one form, it's probably structurally typical of my
                pages) and adjust the addressing of controls; it still works in MSIE4,
                but I cannot readily check that it works elsewhere.

                Forms, as I use them, do split the name-space of the page into separate
                scopes, which is certainly useful in pages which have a number of
                independent scripted-in/out units. Is there another means of doing
                that, known to be widely browser-compatible?

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                Comment

                Working...