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

I'm trying to validate a form I'm creating and can't seem to understand how this works.

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745639095a4637576.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信