dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript Validation using External JS File

  1. #1
    Join Date
    Dec 2008
    Posts
    3

    Javascript Validation using External JS File

    Hi, Firstly, im a newbie to web developement, so please accept my apologies for my basic questions.

    I have the following HTML

    <head>
    <link rel="stylesheet type" type="text/css" href="/My First Website/Inc/CSS/mycss.css"
    <script type="text/javascript" src="/My First Website/Inc/JavaScript/Form.js"> </script><meta charset="utf-8"

    <div>
    <form method="post" form name="HomePageForm">
    First Name : <input type="text" name="firstname"/><br />
    Surname : <input type="text" name="surname" /><br />
    Password : <input type="password"name="pwd" /><br />
    <input type ="submit" value = "Submit" />
    </div>
    </head>


    I have highlihted in Bold, the query i Have.

    I dont know how to reference the form 'HomePageForm' in my javascript file.

    I beleive i have referenced it OK in the HTML file, but i want to do some basic validation such as if the firstname and surname are blank then show a messagebox...
    but im struggling on how to acheive this in the js file.

    Can someone please help.

    Thanks

  2. #2
    Join Date
    Feb 2011
    Location
    Chuluota, Florida
    Posts
    144
    This belongs in the Javascript forum. With that being said take a look at this and see if it helps.

    http://www.w3schools.com/js/js_form_validation.asp

  3. #3
    Join Date
    Apr 2012
    Posts
    1

    Smile

    Quote Originally Posted by hunter101 View Post
    Hi, Firstly, im a newbie to web developement, so please accept my apologies for my basic questions.

    I have the following HTML

    <head>
    <link rel="stylesheet type" type="text/css" href="/My First Website/Inc/CSS/mycss.css"
    <script type="text/javascript" src="/My First Website/Inc/JavaScript/Form.js"> </script><meta charset="utf-8"

    <div>
    <form method="post" form name="HomePageForm">
    First Name : <input type="text" name="firstname"/><br />
    Surname : <input type="text" name="surname" /><br />
    Password : <input type="password"name="pwd" /><br />
    <input type ="submit" value = "Submit" />
    </div>
    </head>


    I have highlihted in Bold, the query i Have.

    I dont know how to reference the form 'HomePageForm' in my javascript file.

    I beleive i have referenced it OK in the HTML file, but i want to do some basic validation such as if the firstname and surname are blank then show a messagebox...
    but im struggling on how to acheive this in the js file.

    Can someone please help.

    Thanks

    Hi Boss

    Place onclick="validateNames();" in Submit Button like below

    <input type="password"name="pwd" /><br />
    <input type ="submit" value = "Submit" onclick="validateNames();" />

    in Js File , place the below function

    function validateNames(){
    if(document.HomePageForm.firstname=="" || document.HomePageForm.surname==""){
    alert("firstName and surName should not be empty");
    return false;
    }
    return true;
    }

    And also, I did not find the action attribute in your form Tag..which is mandatory. Please read about the action tag and place it accordingly.

    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles