Unable to load javascrip

Discussion in 'Javascript' started by Terje Sæternes, Oct 29, 2004.

  1. It seems that this script wont run, any idea what I have don wrong+

    Code-----
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript" type="text/javascript">
    <!--
    function isblank(s) {
    for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
    }



    function SetRegAction() {

    // WebBrukerID
    if (isblank(document.form1.test.value)) {
    alert ("Fornavn må fylles ut.");
    document.form1.test.focus();
    return (false);
    }
    //-->
    </script>
    </head>

    <body>
    <form name="form1" method="post" action="" onsubmit="javascript:return
    SetRegAction()">
    <input name="test" type="text" id="test">
    <input type="submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>
    Terje Sæternes, Oct 29, 2004
    #1
    1. Advertising

  2. Problemo solved, it was the missing "}" aarghhh


    Terje

    "Terje Sæternes" <> skrev i melding
    news:Tfqgd.60823$...
    > It seems that this script wont run, any idea what I have don wrong+
    >
    > Code-----
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    > <script language="javascript" type="text/javascript">
    > <!--
    > function isblank(s) {
    > for (var i = 0; i < s.length; i++) {
    > var c = s.charAt(i);
    > if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    > }
    > return true;
    > }
    >
    >
    >
    > function SetRegAction() {
    >
    > // WebBrukerID
    > if (isblank(document.form1.test.value)) {
    > alert ("Fornavn må fylles ut.");
    > document.form1.test.focus();
    > return (false);
    > }
    > //-->
    > </script>
    > </head>
    >
    > <body>
    > <form name="form1" method="post" action="" onsubmit="javascript:return
    > SetRegAction()">
    > <input name="test" type="text" id="test">
    > <input type="submit" name="Submit" value="Submit">
    > </form>
    > </body>
    > </html>
    >
    >
    Terje Sæternes, Oct 29, 2004
    #2
    1. Advertising

  3. On Fri, 29 Oct 2004 13:58:49 +0200, Terje Sæternes <>
    wrote:

    [snip]

    > <script language="javascript" type="text/javascript">


    The language attribute is deprecated, and the (required) type attribute
    makes it redundant. Remove it.

    > <!--


    Hiding SCRIPT element content is an obsolete practice. All user agents now
    in use understand what a SCRIPT element is, even if they cannot execute
    it. Scripts should usually be placed in external files anyway, hiding the
    script automatically.

    > function isblank(s) {
    > for (var i = 0; i < s.length; i++) {
    > var c = s.charAt(i);
    > if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    > }
    > return true;
    > }


    This is a really inefficient way of performing this sort of test. Use a
    regular expression:

    function isBlank(v) {
    return /^\s*$/.test(v);
    }

    The regular expression will match any string that is either empty, or
    solely composed of whitespace characters[1].

    > function SetRegAction() {


    Submission handlers should generally be written to take one argument: a
    reference to the form. This means you don't have to hard-code the form
    name.

    function SetRegAction(form) {
    // ...

    You'd call this from the FORM with:

    <form ... onsubmit="SetRegAction(this);">

    Note that you don't need "javascript:". It only makes a difference in one
    browser (IE), and even then, its only necessary if you've been using
    scripting languages other than Javascript. Most browsers will see that
    string a label, and ignore it.

    Based on this change, you could modify the following to:

    // Save a reference to the 'test' control
    var test = form.elements['test'];

    > if (isblank(document.form1.test.value)) {


    if(isBlank(test.value)) {

    > alert ("Fornavn må fylles ut.");
    > document.form1.test.focus();


    test.focus();

    > return (false);


    return false;

    The parentheses aren't necessary.

    > }


    [snip]

    > <form name="form1" method="post" action=""


    Now you don't need to name the form (you should use better names, anyway).

    > onsubmit="javascript:return SetRegAction()">
    > <input name="test" type="text" id="test">


    You do realise that this control wouldn't be submitted, don't you?

    > <input type="submit" name="Submit" value="Submit">


    Unless you have multiple submit buttons which represent different actions,
    you generally don't need to name them. In such situations, something like:

    <input type="submit" name="operation" value="Delete">

    would be more appropriate. Certainly be careful not to use names that are
    properties of the FORM object. Naming a control, action, for example, will
    prevent you from accessing the action property on the FORM.

    [snip]

    Hope that helps,
    Mike


    [1] A more thorough explanation:

    Regular expressions are composed of a series of productions that form a
    pattern. Certain characters have special meanings, causing the pattern to
    evaluated in a certain way. In the expression I used, there are four
    tokens, each of which is special.

    The first, ^, means that the pattern that follows must match the beginning
    of the input. So, for example, ^some would match any string that began
    with "some"; "something", "sometime", "some chicken", etc.

    The last, $, means that the pattern that precedes the token must match the
    end of the input.

    The escape sequence, \s, matches whitespace characters.

    The token, *, is a quantity specifier that means the preceding token can
    exist zero or more times.

    All together, you get a pattern that will match an empty string, or one
    which contains only whitespace.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Oct 29, 2004
    #3
  4. Terje Sæternes

    Grant Wagner Guest

    "Terje Sæternes" wrote:

    > It seems that this script wont run, any idea what I have don wrong+
    >
    > Code-----
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    > <script language="javascript" type="text/javascript">


    <script type="text/javascript">

    The -language- attribute is deprecated and not required.

    > <!--


    Not needed. Remove.

    > function isblank(s) {
    > for (var i = 0; i < s.length; i++) {
    > var c = s.charAt(i);
    > if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    > }
    > return true;
    > }
    >
    > function SetRegAction() {
    >
    > // WebBrukerID
    > if (isblank(document.form1.test.value)) {
    > alert ("Fornavn må fylles ut.");
    > document.form1.test.focus();
    > return (false);
    > }


    You're missing a closing -}- here.

    > //-->


    Not needed. Remove.

    > </script>
    > </head>
    >
    > <body>
    > <form name="form1" method="post" action="" onsubmit="javascript:return
    > SetRegAction()">


    onsubmit="return SetRegAction();"

    > <input name="test" type="text" id="test">
    > <input type="submit" name="Submit" value="Submit">


    This is fine, you've named your submit button "Submit". Be careful though,
    if you named your submit button "submit" (note the lower case 's') you could
    run into problems later if you attempt to submit the form programmatically
    (ie - document.forms['theForm'].submit()).

    > </form>
    > </body>
    > </html>


    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Oct 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Ladd

    Disable Calendar Control from javascrip

    Robert Ladd, Feb 27, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,269
    Robert Ladd
    Feb 27, 2005
  2. =?utf-8?B?4pmlZVh0cmVtZSBvWHlnZW7imaU=?=

    How do i call JAVASCRIP function if...

    =?utf-8?B?4pmlZVh0cmVtZSBvWHlnZW7imaU=?=, Aug 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    477
    =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmE=?=
    Aug 25, 2006
  3. Ganesh
    Replies:
    1
    Views:
    442
    clintonG
    Aug 2, 2007
  4. deepak kumar
    Replies:
    0
    Views:
    139
    deepak kumar
    Oct 30, 2004
  5. Alex Maghen

    "ASP:" WebControls and Non-JavaScrip

    Alex Maghen, Feb 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    158
    Scott M.
    Feb 9, 2005
Loading...

Share This Page