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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form validation empty field js

    [CODE]
    <HTML>
    <HEAD>
    <script language="JavaScript" type="text/JavaScript">
    <script language='Javascript'>
    function validateindex()
    {

    var x = document.forms["index"]["student_id"].value;

    if (x==null, || x == "")
    {
    alert("Please fill out the 'user name' field...");
    return false;
    }
    }
    </script>
    <link rel='stylesheet' href='include/styles.css' type='text/css'>
    </HEAD>


    <BODY bgcolor='#ffffee' link='#12619E' vlink='#12619E' alink='#12619E'>


    <DIV align='center'><IMG src='images/logo_trans_95.gif' height="105" border=0>
    <BR/><A HREF="admin">Admin Login</A><BR/>
    </DIV>

    <FORM action = 'showData.php' name='form' method='POST' >


    <TABLE align='center' border=1 cellpadding=2 cellspacing=2
    bordercolor='#1E56A0' bgcolor='#FFFFFF' class='bodytext' width=25%>
    <TH colspan=2>Grades</TH>
    <TR><TD align='right'>
    Student ID:<BR><BR>
    </TD><TD>
    <INPUT TYPE='text' SIZE=10 MAXLENGTH=10 name='student_id'>
    </TD>
    </TR>
    <TR><TD colspan=2 align='center'>
    <INPUT TYPE='submit' value="submit" name="submit">
    </TD>
    </TR>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>
    [code]

    i have make the part bold which i used to try to validate it,still its not working? any ideas?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Nowhere are you calling the function. And there are other errors too.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validateindex()
    {
    
    var x = document.forms[0]["student_id"].value;
    
    if (x==null || x == "")
    {
    alert("Please fill out the 'user name' field...");
    return false;
    }
    }
    </script>
    <link rel='stylesheet' href='include/styles.css' type='text/css'>
    </HEAD>
    
    <BODY bgcolor='#ffffee' link='#12619E' vlink='#12619E' alink='#12619E'>
    
    
    <DIV align='center'><IMG src='images/logo_trans_95.gif' height="105" border=0>
    <BR/><A HREF="admin">Admin Login</A><BR/>
    </DIV>
    
    <FORM action = 'showData.php' method='POST' onsubmit="validateindex();">
    
    
    <TABLE align='center' border=1 cellpadding=2 cellspacing=2
    bordercolor='#1E56A0' bgcolor='#FFFFFF' class='bodytext' width=25%>
    <TH colspan=2>Grades</TH>
    <TR><TD align='right'>
    Student ID:<BR><BR>
    </TD><TD>
    <INPUT TYPE='text' SIZE=10 MAXLENGTH=10 name='student_id'>
    </TD>
    </TR>
    <TR><TD colspan=2 align='center'>
    <INPUT TYPE='submit' value="submit" >
    </TD>
    </TR>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>
    And much of the HTML is old code, to the point of being quaint.
    Last edited by DrDOS; 09-17-2013 at 05:08 AM. Reason: Found another error in the if(x==null || x=="") statement there was a comma.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,216
    Thanks
    12
    Thanked 338 Times in 334 Posts
    note, form field values are never ever null. they are a string, or—in case they are not form elements—undefined. besides that are null and "" equal in a type-less comparison ((null == "") // true (null === "") // false)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    A single space is sufficient to get past that test.

    Also the alert call should only be used for debugging. You should update a message in the page itself using innerHTML for the live page.

    JavaScript is best attached just before the </body> tag.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    if (x==null, || x == "")

    A form field value can never be null.

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
    Numeric values, such as zip codes, phone numbers and dates, should be validated as such. Ditto email addresses.
    This topic has been covered many times before in this forum.

    <script language="JavaScript" type="text/JavaScript">
    <script language='Javascript'>
    That duplication will prevent your script from running. <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

    The closing code tag is /code

    Quizmaster: How many centimetres are there in 3.5 metres?
    Contestant: 115
    Last edited by Philip M; 09-17-2013 at 08:19 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,216
    Thanks
    12
    Thanked 338 Times in 334 Posts
    Quote Originally Posted by felgall View Post
    JavaScript is best attached just before the </body> tag.
    I’d still attach library code in the <head>
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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