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

    Problom With Validation Script

    Hi guys, i have a little problom with Check Function

    *********************************************************

    <form method="POST" id="SearchBox" name="SearchBox" action="Search.php" onsubmit="return Scheck()">
    <input type="text" name="Sname" size="20" dir="rtl" style="width:100%; height:21px;" class="textbox" onkeydown="if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document.SearchBox.Search.click(); return false; } else return true;">
    <input type="image" src="Images/MainSite/SearchButton.jpg" name="Search" value="Submit" width="66" height="24">
    </form>

    This was the form... now look.. onsubmit = Scheck();
    Scheck is a function that checks if the field is empty or not.

    here is the js for Scheck:

    <script>
    function Scheck() {
    if (SearchBox.Sname.value=='') {
    alert("Search Box Is Empty!!!");
    SearchBox.Sname.focus();
    return false;
    }
    SearchBox.submit();
    }
    </script>




    Now.. what should be going on is this:
    someone write down some **** inside the textbox.. if he press Enter button (value = 13) -> the submit button inside the form is clicked (good!). when it's clicked = form is submit. the form then sends it to Scheck() (js function)
    because onsubmit = scheck()...

    but inside the site, when you press Enter key and the input text field is empty, you get some kind of error related
    to the submit button (document.SearchBox.Search is empty or not NULL).

    http://www.ForEver.co.il/V1/

    Hope you'll know better then i do
    Last edited by JsHelp; 08-25-2007 at 09:38 PM. Reason: Changed Code

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Do you really need all that code in the onkeypress event handler?

    Whether you click the submit button or press enter the onsubmit event handler will be called so I don't see why you need to do anything different if the enter key is pressed.

    Also you should change this line:
    Code:
    SearchBox.submit();
    to simply
    Code:
    return true;
    in your Scheck function.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Give this a try:
    Code:
    <script type="text/javascript">
    function Scheck(frm, field) {
    if (frm[field].value === '') {
    alert('Search Box Is Empty!!!');
    frm[field].focus();
    return false;
    } else {
    return true;
    }
    }
    document.onkeydown = function(evt) {
    var evt  = (evt || event) || null;
    var node = (evt.target || evt.srcElement) || null;
    if ((evt.keyCode == 13) && (node.type == "text")) {
    return false;
    }
    }
    </script>
    
    <form method="POST" id="SearchBox" name="SearchBox" action="Search.php" onsubmit="return Scheck(this,'Sname')">
    <input type="text" name="Sname" size="20" dir="rtl" style="width:100%; height:21px;" class="textbox">
    <input type="image" src="Images/MainSite/SearchButton.jpg" name="Search" value="Submit" width="66" height="24">
    </form>


  •  

    Posting Permissions

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