Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts

    javascript validation error

    Hi,

    got this from the jsfilddle.

    it does not work, whereas on jsfiddle ..it works fine..am I missing something
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testjs.aspx.cs" Inherits="testjs" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <style type="text/css">
    input {
        padding:5px;
        border:1px solid #AA0;
        font-size:11px;
        margin:5px;
    }
    .pleasefillfield {
        background:green;
        color:white
    }
    </style>
    <script type="text/javascript" src="Scripts/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js"></script>
    <script type="text/javascript">
        var doClear = function(field) { if (field.value == field.defaultValue) { field.value = ""; $(field).removeClass('pleasefillfield') } };
        var doDefault = function(field) { if (field.value == "") { field.value = field.defaultValue; $(field).addClass('pleasefillfield') } };
    
        $('input[type="text"]').focus(function() {
            doClear(this);
        })
    
        $('input[type="text"]').blur(function() {
            doDefault(this);
        })
    
        $('input[type="text"]').each(function() {
            this.defaultValue = "please enter " + $(this).attr('name');
        })
    
        $('form').submit(function() {
            var errors = 0;
            var form = this;
            $('input[type="text"]').each(function() {
                if (this.value == "" || this.value == this.defaultValue) {
                    errors += 1;
                    doDefault(this);
                }
            })
    
            if (!errors) {
                alert('Ok');
                //form.submit()   
            } else {
                alert("errors: " + errors);
            }
    
            return false;
        })    
    
    </script>
        <title></title>
    </head>
    <body>
        <form action="#" method="post">
        <input type="text" name="username" value="" size="30" />
        <br/>
        <input type="text" name="userpassword" value="" size="30" />
        <br/>
        <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Do you have the jQuery to load, locally?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •