dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need help with Security Questions

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Question Need help with Security Questions

    Hi, I am currently having a problem with my code. It is pointing at a logical error somewhere as neither the error console in FireFox nor Firebug can detect any errors. The problem is getting the alert box to pop up when the user types in invalid letters or numbers for their respective functions. I guess it could be the Unicode coding for Internet Explorer or Mozilla, or maybe the validation is not getting called from the option list....Anyway here's my code so far:



    <script type="text/javascript">
    /* <![CDATA[ */

    function validateAlphabetic(keyPressEvent) {
    if (navigator.appName == "Microsoft Internet Explorer")
    var enteredKey = keyPressEvent.keyCode;
    else if (navigator.appName == "Netscape")
    var enteredKey = keyPressEvent.charCode;
    var enteredChar = String.fromCharCode(enteredKey);
    var retValue = true;
    try {
    if (!/\D/.test(enteredChar) && !/\W/.test(enteredChar))
    throw "You did not enter an alphabetic value.";
    }
    catch(inputError) {
    window.alert("You can only enter letters into this field.");
    retValue = false;
    }
    finally {
    return retValue;
    }
    }
    function validateNumeric(keyPressEvent) {
    if (navigator.appName == "Microsoft Internet Explorer")
    var enteredKey = keyPressEvent.keyCode;
    else if (navigator.appName == "Netscape")
    var enteredKey = keyPressEvent.charCode;
    var enteredChar = String.fromCharCode(enteredKey);
    var retValue = true;
    try {
    if (!/\d/.test(enteredChar) && !/\W/.test(enteredChar))
    throw "You did not enter a numeric value.";
    }
    catch(inputError) {
    window.alert("You can only enter numbers into this field.");
    retValue = false;
    }
    finally {
    return retValue;
    }
    }

    /* ]]> */
    </script>
    </head>

    <body>
    <h1>Challenge Questions</h1>
    <form action="" enctype="application/x-www-form-urlencoded">
    <select>
    <option value="maiden" onkeypress="return validateAlphabetic(event)">What is your mother's maiden name</option>
    <option value="pet" onkeypress="return validateAlphabetic(event)">What is the name of your favorite pet?</option>
    <option value="city" onkeypress="return validateAlphabetic(event)">What city were your born in?</option>
    <option value="security" onkeypress="return validateNumeric(event)">What is your social security number?</option>
    <option value="siblings" onkeypress="return validateNumeric(event)">How many siblings do you have?</option>
    </select><br />
    <input type="text" size="25" />
    </form>

    </body>

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    You might try putting the onkeypress event on the text input instead of the options, where it isn't doing anything.

  3. #3
    Join Date
    Apr 2012
    Posts
    2
    Quote Originally Posted by justinbarneskin View Post
    You might try putting the onkeypress event on the text input instead of the options, where it isn't doing anything.
    Yes that is part of the solution, however just putting it in the input area, will not run the script, I need to figure out another function with an if/else statement to include the items in the selection list. This is what I have currently. It still does not run the script even with the new function added.


    function validateAlphabetic(keyPressEvent) {
    if (navigator.appName == "Microsoft Internet Explorer")
    var enteredKey = keyPressEvent.keyCode;
    else if (navigator.appName == "Netscape")
    var enteredKey = keyPressEvent.charCode;
    var enteredChar = String.fromCharCode(enteredKey);
    var retValue = true;
    try {
    if (!/\D/.test(enteredChar) && !/\W/.test(enteredChar))
    throw "You did not enter an alphabetic value.";
    }
    catch(inputError) {
    window.alert("You can only enter letters into this field.");
    retValue = false;
    }
    finally {
    return retValue;
    }

    function validateNumeric(keyPressEvent) {
    if (navigator.appName == "Microsoft Internet Explorer")
    var enteredKey = keyPressEvent.keyCode;
    else if (navigator.appName == "Netscape")
    var enteredKey = keyPressEvent.charCode;
    var enteredChar = String.fromCharCode(enteredKey);
    var retValue = true;
    try {
    if (!/\d/.test(enteredChar) && !/\W/.test(enteredChar))
    throw "You did not enter a numeric value.";
    }
    catch(inputError) {
    window.alert("You can only enter numbers into this field.");
    retValue = false;
    }
    finally {
    return retValue;
    }

    function keyPressed(pressEvent)
    {
    var choice = document.forms[0].whichQuestion.value;
    if ( choice == "security" || choice == "siblings")
    {
    return validateNumeric(pressEvent);
    } else {
    return validateAlphabetic(pressEvent);
    }

    var choose = document.forms[0].whichQuestion.value;
    if ( choose == "city" || choose == "pet" || choose == "maiden")
    {
    return validateAlphabetic(pressEvent);
    } else {
    return validateNumeric(pressEvent);
    }

    }
    /* ]]> */
    </script>
    </head>

    <body>
    <h1>Challenge Questions</h1>
    <form action="" enctype="application/x-www-form-urlencoded" onsubmit="return validate(this);" />
    <select name="whichQuestion">
    <option value="maiden">What is your mother's maiden name</option>
    <option value="pet">What is the name of your favorite pet?</option>
    <option value="city">What city were your born in?</option>
    <option value="security">What are the last four digits of your social security number?</option>
    <option value="siblings">How many siblings do you have?</option>
    </select><br />
    <input type="text" name="answer" size="25" onkeypress="return keyPressed(event);" />

    </form>

    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles