www.webdeveloper.com
Results 1 to 7 of 7

Thread: Variable illegalChar doesn't clear

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    7

    Variable illegalChar doesn't clear

    I want the following function to validate a user input. It's fairly crude for the moment but essentially I want to prohibit the user from either leaving the input blank, or from entering single quotes that truncate the user input when it goes into sql.

    The code is as follows:

    function addCommodCheck() {
    var txtAddCommodity = O('txtAddCommodity').value
    var illegalChar = false

    for (i=0; i<txtAddCommodity.length; i++) {
    if (txtAddCommodity.charAt(i) == '-')
    illegalChar = true
    }

    if (txtAddCommodity == '')
    alert("You must input a Commodity before proceeding")
    else if (illegalChar = true) {
    alert("You cannot use the '-' sign in your Commodity name")
    illegalChar = false
    }
    }

    I'm not actually sure how to sandwich a single quote within two other single quotes so for now I've used a dash - I'm sure I can figure that out but if someone knows I'd appreciate being told. This isn't the main problem however.

    The main problem I'm having is as follows:

    Consider the scenario that the user does enter a Dash. This is picked up by the script and the user is informed. The user then removes the dash. However when they next click the button to validate their input they are told once again that there is a dash in the input value.

    For some reason the 'illegalChar' variable is not clearing and I'm confused as to why?

  2. #2
    Join Date
    Jul 2013
    Posts
    7
    Having looked at this a little further, the dash is being picked up even before I've input it for the first time and illegalChar is being set to true. Can someone explain why that's happening?

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Use a '\'' (with a backslash before the single quote) or "'" (with two double quotes for delimiters) for a single quote and call the function addCommodCheck with a onblur on this input.
    An other solution, with utf-8, consists to use the true apostrophe (alt 0146) and to replace all singles quotes with this apostrophe with something like :

    Code:
    var txtAddCommodity = O('txtAddCommodity').value;
    var newTxtAddCommodity = txtAddCommodity.replace(/'/g,'');

  4. #4
    Join Date
    Jul 2013
    Posts
    7
    Thanks for the help.

    The main issue however was

    Code:
    The main problem I'm having is as follows:
    
     Consider the scenario that the user does enter a Dash. This is picked up by the script and the user is informed. The user then removes the dash. However when they next click the button to validate their input they are told once again that there is a dash in the input value. 
    
     For some reason the 'illegalChar' variable is not clearing and I'm confused as to why?

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Put a var before illegalChar in your function to transform this variable in a local variable and recall the function with an event onblur or onsubmit !

  6. #6
    Join Date
    Jun 2008
    Posts
    106
    If I were you, I'd use an event listener to prevent them from even typing a quote/dash.

    Here's an example...

    Code:
    <input type="text" value="" id="commodity" size="50" />
    Code:
    function handle_values(event) {
        var key = event.charCode ? event.charCode : (event.keyCode ? event.keyCode : 0);
    
        // 222 = quote key
        if (key === 222) {
            alert('You cannot enter quotes.');
    
            // remove all quotes from the field (they can hack these in, so it's good to check)
            // you may also want to prevent backslashes, for security
            event.target.value = event.target.value.replace(/['"]+/g, '');
    
            // prevent them from entering this key
            event.preventDefault();
        }
    }
    
    document.getElementById('commodity').addEventListener('keydown', handle_values, false);

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by stoolpigeon View Post
    else if (illegalChar = true) {
    You meant to type "==" for comparison. Otherwise, a single "=" means assignment. That's why illegalChar is always true.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

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