#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New York
    Posts
    89
    Rep Power
    14

    Form Validation in JavaScript


    ok guys I am moron when it comes to JS.. php is my thing. A freinds wesite that he uses for his buisness offers a free chat room ,but he wants to reserve one Nick Name for himself so his clients know they are talking to him. So I want to validate the form before it is submitted and if the nickname they choose is his pop an alert saying choose another nickname. Here is the script I am trying to use.. Its something I found on the net and tried to modify

    In the Head I have:

    var user = "Great4u";

    function validate(f)
    {
    if (f.nickname.value = user)
    {
    alert("You cannot use this Nick Name! Please choose another.");
    return false;
    }
    return true;
    }

    The Form:

    <form name=chat target="entropychat" action="http://myurl.com/" onSubmit="return validate(chat)";>
    Nick Name: <input name="nickname">
    <input type="hidden" name="channel" value="myurl.com">
    <input type="submit" value="Enter Chat">
    </form>



    If I enter a Nick Name it blows an error and puts the nick I don't want them to use as the value for the feild.

    Thanks in advance for your help.. I am over a barrel with this one.
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    var user = 'Great4u';
    function validate(f) {
    if (f.nickname.value == user) {
    alert('You cannot use this Nickname! Please choose another.');
    f.nickname.value = '';
    f.nickname.focus();
    return false;
    }
    return true;
    }


    <form name="chat" target="entropychat" action="http://myurl.com/" onsubmit="return validate(this)">
    Nick Name: <input name="nickname" type="text">
    <input name="channel" type="hidden" value="myurl.com">
    <input type="submit" value="Enter Chat">
    </form>

    [comparison operator, not assignment]

IMN logo majestic logo threadwatch logo seochat tools logo