Seek javascript validator

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

    Seek javascript validator

    Is there a way to confirm that a string has valid Javascript syntax?

    Either a client side or server side solution would be ok.

    For a client side solution I'd like to confirm that, say :

    var a = "function foo { var t='ok'; alert(t); }"

    is valid, or for a server side solution that

    String a = "function foo { var t='ok'; alert(t); }"

    is valid.

    Thanks
  • Stewart Gordon

    #2
    Re: Seek javascript validator

    GIMME wrote:
    [color=blue]
    > Is there a way to confirm that a string has valid Javascript syntax?
    >
    > Either a client side or server side solution would be ok.
    >
    > For a client side solution I'd like to confirm that, say :
    >
    > var a = "function foo { var t='ok'; alert(t); }"
    >
    > is valid, or for a server side solution that
    >
    > String a = "function foo { var t='ok'; alert(t); }"[/color]

    Neither is client-side or server-side.

    The first vaguely resembles JavaScript, and the second vaguely resembles
    Java.

    Each has both client-side and server-side forms.

    But does your crosspost mean that you really are looking for a solution
    in either language?

    Stewart.

    --
    My e-mail is valid but not my primary mailbox, aside from its being the
    unfortunate victim of intensive mail-bombing at the moment. Please keep
    replies on the 'group where everyone may benefit.

    Comment

    • GIMME

      #3
      Re: Seek javascript validator

      Yes. Either language. Java or Javascript.

      But the language can not be server-side Javascript (old netscape Livewire).
      [color=blue]
      > But does your crosspost mean that you really are looking for a solution
      > in either language?[/color]

      Comment

      • rh

        #4
        Re: Seek javascript validator

        gimme_this_gimm e_that@yahoo.co m (GIMME) wrote:[color=blue]
        > Is there a way to confirm that a string has valid Javascript syntax?
        >
        > Either a client side or server side solution would be ok.
        >
        > For a client side solution I'd like to confirm that, say :
        >
        > var a = "function foo { var t='ok'; alert(t); }"[/color]

        The following sample code should provide a check for valid Javascript
        syntax.

        Note that it could be greatly simplified if you are able to assume the
        browser supports try/catch. If not, and the browser (e.g., IE 4.0,
        Navigator 3.0) supports window.onerror, it is used in an attempt to
        give a slightly more graceful failure. In the latter case,
        window.onerror could be further exploited to provide support for the
        validation.

        validate = null;
        validateFunctio nText =
        ' try {'
        + ' var f = new Function(str);'
        + ' return "Validated OK";'
        + ' }'
        + ' catch (e) {'
        + ' return "Validation failed: "+(e.descriptio n '
        + ' || e.message || "");'
        + ' }';

        function catchError(msg) {
        return ! alert("Failed to create validator: "+(msg || "")) ;
        }

        if (typeof window.onerror != "undefined" ) {
        onerrorSave = window.onerror;
        window.onerror = catchError;
        validate = new Function("str", validateFunctio nText);
        window.onerror = onerrorSave;
        }
        else validate = new Function("str", validateFunctio nText);

        // Test

        var a = "function foo { var t='ok'; alert(t); }" ;

        if(validate) alert( validate(a)) ;
        else alert("Unable to validate Javascript");

        ../rh
        [color=blue]
        >
        > Thanks[/color]

        Comment

        Working...