javascript - Check textbox before submitting - Stack Overflow

How could I check a textbox before submition?<form action='search.php' method='GET�

How could I check a textbox before submition?

<form action='search.php' method='GET'>

         <input type="text" id= "q" name="q" class='textbox' >

         <input type="submit" id='submit' value="Search" class ='button' >

     </form>

How could I check a textbox before submition?

<form action='search.php' method='GET'>

         <input type="text" id= "q" name="q" class='textbox' >

         <input type="submit" id='submit' value="Search" class ='button' >

     </form>
Share Improve this question edited Jul 12, 2019 at 7:19 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Apr 14, 2013 at 9:34 ConnorConnor 6451 gold badge7 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Try this simple with JavaScript validation:

<html>
    <head>
        <script type="text/javascript">
            function check()
            {
                var searchtext = document.getElementById("q").value;
                if(searchtext=='')
                {
                    alert('Enter any character');
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <form  action='search.php' method='GET' onSubmit="return check();">
            <input type="text" id= "q" name="q" class='textbox' >
            <input type="submit" id='submit' value="Search" class ='button' >
        </form>
    </body>
</html>
<?php
if(!empty($_GET['q']))
{
//if it is not empty, do something
}
else
{
//otherwise do this
}
?>

May I also remend using POST rather than GET if you're sending something from a form.

You can use jQuery as in the following:

$(function(){
    $('#submit').click(function(e){
        if($('#q').val() != null && $('#q').val().length > 0){
            $.get('search.php', $('#formId').serialize(), function(result){});
        }
        else{
            //Otherwise do this
        }
        e.preventDefault();
    });
});

You can use some stylish jQuery library for easy coding like jQuery validation plugin.

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

相关推荐

  • javascript - Check textbox before submitting - Stack Overflow

    How could I check a textbox before submition?<form action='search.php' method='GET�

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信