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

    Similar to a previous question but how do I prevent ctrl+someKey combinations?

    The code I will am including is far from finished, but some of what I do have works (the "submit" code does not work yet). I am able to prevent certain kinds of input including Shift-Ins pasting, but Ctrl-v pasting still works (and I do not want it to). Could you please tell/show me what code needs to be entered to my existing functions, or what new function I need to create (and call from my existing functions) to eliminate any kind of CtrlKey input?
    Thanks in advance,
    mitchellA

    <code>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> Mitchell's Test Form </title>
    <meta name="Generator" content="Alleycode HTML Editor">
    <meta name="Description" content="Your description here...">
    <meta name="Keywords" content="Your keywords here...">
    <script type="text/javascript">
    function is_it_A (e)
    {// allows only a-z, A-Z, <backspace>, <space>, apostrophe, and hyphen
    var a;
    document.all ? a = e.keycode : a = e.which;
    return ((a > 64 && a < 91) || (a > 96 && a < 123) || a == 8 || a == 32 || a == 39 || a == 45);
    }
    function is_it_P (w)
    {// allows only a-z, A-Z, 0-9, <backspace>, hyphen, and underscore
    var p;
    document.all ? p = w.keycode : p = w.which;
    return ((p > 47 && p < 58) || (p > 64 && p < 91) || (p > 96 && p < 123) || p == 8 || p == 45 || p == 95);
    }
    function is_it_N (i)
    {// allows only 0-9, <backspace>
    var n;
    document.all ? n = i.keycode : n = i.which;
    return ((n > 47 && n < 58) || n == 8);
    }
    function is_it_T (p)
    {// allows only 0-9, <backspace>, <right & left brackets()>, and hyphen
    var t;
    document.all ? t = p.keycode : t = p.which;
    return ((t > 47 && t < 58) || t == 8 || t == 40 || t == 41 || t == 45);
    }
    function is_it_E (m)
    {// allows only 0-9, a-z, A-Z, <backspace>, hyphen, <period (.)>, <at (@) sign>, and underscore
    var e;
    document.all ? e = m.keycode : e = m.which;
    return ((e > 47 && e < 58) || (e > 64 && a < 91) || (e > 96 && a < 123) || e == 8 || e == 45 || e == 46 || e == 64 || e == 95);
    }
    function xA (str)
    {
    return str.replace (/[^a-z\'\-\&#32]/gi,"");
    }
    function xN (str)
    {
    return str.replace (/[^0-9]/gi,"");
    }
    function xT (str)
    {
    return str.replace (/[^0-9\(\)\&#32]/gi,"");
    }
    function xE (str)
    {
    return str.replace (/[^a-z\0-9\-\_\&#46\&#64\&#95]/gi,"");
    }

    function validateFormOnSubmit (theForm)
    {
    var f=document.forms["theForm"]["fname"].value
    var l=document.forms["theForm"]["lname"].value
    var p=document.forms["theForm"]["pwd"].value
    var e=document.forms["theForm"]["eml"].value
    if (f==null || f=="")
    {
    fld.style.background = "red";
    alert = ("Please enter your First Name.");
    return false;
    }
    else if (l==null || l=="")
    {
    fld.style.background = "red";
    alert = ("Please enter your Last Name.");
    return false;
    }
    else if (p==null || p=="")
    {
    fld.style.background = "red";
    alert = ("Please enter your Password.");
    return false;
    }
    else if (e==null || e=="")
    {
    fld.style.background = "red";
    alert = ("Please enter your e-mail address.");
    return false;
    }
    else
    {
    fld.style.background = "white";
    }
    }


    </script>
    </head>

    <body>
    <CENTER>
    <FORM name="theForm" onsubmit="return validateFormOnSubmit ()">
    <H1>Please supply the following data...</H1>
    <br />
    <FONT size="5">
    <label for="fname">First Name: </label><INPUT type="text" onkeypress="return is_it_A(event)" onblur="this.value=xA(this.value)" name="firstname" id="fname"><br />
    <br />
    <label for="lname">Last Name: </label><INPUT type="text" onkeypress="return is_it_A(event)" onblur="this.value=xA(this.value)" name="lastname" id="lname"><br />
    <br />
    <label for="pwd">Password: </label><INPUT type="text" onkeypress="return is_it_P-(event)" onblur="this.value=xA(this.value)" name="password" id="pwd"><br />
    <br />
    <label for="eml">E-mail Address: </label><INPUT type="text" onkeypress="return is_it_E(event)" onblur="this.value=xA(this.value)" name="email" id="eml"><br />
    <br />
    <input name="info" type="submit" value="Done>>">
    </font>
    </CENTER>

    </FORM>
    </body>
    </html>

    </code>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    
    if( ev.ctrlKey) {
    alert ("You pressed the Control key");
    }
    }
    
    </script>

    Note that the code tags should be upper-case.

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan
    Last edited by Philip M; 02-03-2011 at 07:33 AM.

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Still confused.

    <CODE>
    function is_it_N (i)
    {// allows only 0-9, <backspace>
    var n;
    document.all ? n = i.keycode : n = i.which;
    return ((n > 47 && n < 58) || n == 8);
    }
    </CODE>

    I was under the impression that IE used "keycode" and Mozilla used "which", so shouldn't both be used in your example? Also, my form calls functions like the one above using "keypress" and I do not know how to call your "keydown" function as well as my function at the same time (I know nesting functions is possible, and calling functions within functions is possible, but I do not understand how to do that yet).
    Could you please provide a little more information on how to do this and keep in mind I need the same parameters (i.e. keypress, document.all, etc.). Also, I do not want to "alert" anyone, I just want to make using the ctrl key impossible.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Did you try out the script I gave you in Post#2? If so you would have found that it worked fine to disable the control key in all browsers.

    The alert was purely for testing purposes. You should replace this by whatever code you want to execute if the control key is pressed. If nothing at all (i.e. disregard control key) replace that line by
    return false;

    If you only want to detect whether the user has pressed a key, use the onkeydown event instead of onkeypress, because it works for all key types. Both IE and Firefox do not support onkeypress for the ALT, CTL and SHIFT keys.

    But your tests for valid input would be better made using regular expressions rather than checking the keycodes. Your test for email address allows absurd input such as @@@@@....@@@@@

    Code such as if (f==null || f=="") { returns false (i.e. passes the validation) if the entry is a single space or a ?. That makes it virtually useless.

    Your functions which strip invalid characters from a field onblur are pointless when you have already blocked invalid characters onkeypress.

    fld.style.background = "red"; // should be fld.style.backgroundColor = "red";



    Form validation has been covered a zillion times in this forum. The search feature is there to guide you. I do recommend that you do some research and then revise your whole approach to validating your form.
    Last edited by Philip M; 02-06-2011 at 08:57 AM.

  • #5
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    I'll do as you suggest

    Thanks for the info and the "heads-up" about searching for similar posts. I was not trying to annoy, but simply asking newbie questions (I got half way through an HTML/Javascript course last year and am completing it now). The code I am trying to patch together comes from free scripts (some of which are more than a decade old), tutorials, and answers recieved from asking questions in forums (I bought a book on javascript, but it has been less than informative. I was a "dummy" to buy it.).
    As for absurd entries, yes but further down are tests for the proper format (bad examples I am trying to clean up) and eventually when I learn how to call other functions I will call or incorporate the tests.


  •  

    Posting Permissions

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