最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Javascript form validation not empty or no whitespaces - Stack Overflow

matteradmin6PV0评论

I'm trying to validate a form I'm creating and can't seem to understand how this works. I'm trying to make sure the field isn't empty or contains no white-spaces I have validated it server side but not client side.

Could someone please show me the code like below to validate against being empty or having no white-spaces?

I see these below and this is what I thought they did:

x===null // means if field is empty
x===""  // on trying this means if the field is empty
x===" " // and this checks if there is 1 white space

<!DOCTYPE html>
    <html>
    <head>
    <script>
    function validateForm() {
        var x = document.forms["myForm"]["fname"].value;
        if (x===null || x===""|| x===" ") {
            alert("First name must be filled out");
            return false;
        }
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    </body>
    
    </html>

I'm trying to validate a form I'm creating and can't seem to understand how this works. I'm trying to make sure the field isn't empty or contains no white-spaces I have validated it server side but not client side.

Could someone please show me the code like below to validate against being empty or having no white-spaces?

I see these below and this is what I thought they did:

x===null // means if field is empty
x===""  // on trying this means if the field is empty
x===" " // and this checks if there is 1 white space

<!DOCTYPE html>
    <html>
    <head>
    <script>
    function validateForm() {
        var x = document.forms["myForm"]["fname"].value;
        if (x===null || x===""|| x===" ") {
            alert("First name must be filled out");
            return false;
        }
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    </body>
    
    </html>

Share Improve this question edited Jan 24, 2020 at 19:58 bad_coder 13k20 gold badges56 silver badges88 bronze badges asked Dec 18, 2014 at 9:52 NathanNathan 4994 silver badges16 bronze badges 3
  • did you solve your problem? – Amy Commented Dec 18, 2014 at 10:03
  • i have voted up this to – Nathan Commented Dec 18, 2014 at 10:16
  • 1 You ask for code that checks for “no whitespace”, but you have accepted an answer that does not do that. You have not specified what should actually be checked. You do not specify how your code fails to do what you want. You should generally describe data validation in terms of what the data should contain and what it should be like, rather than inpletely describing what it should not be. – Jukka K. Korpela Commented Dec 18, 2014 at 10:28
Add a ment  | 

1 Answer 1

Reset to default 3

You can do this using javascript trim() function.

<!DOCTYPE html>
    <html>
    <head>
    <script>
    function validateForm() {
        var x = document.forms["myForm"]["fname"].value;
        if (x.trim()==null || x.trim()==""|| x===" ") {
            alert("First name must be filled out");
            return false;
        }
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    </body>
    
    </html>

Post a comment

comment list (0)

  1. No comments so far