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 6 of 6

Thread: (e) problem

  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (e) problem

    Hi,

    Currently i'm using a function to check the user from entering special characters in the textbox as below

    function IsAlphaNumber(e)
    {
    // Logic
    }

    In a testing machine if fails by allowing the special characters and show object expected errors.

    Can any one suggest me any work around.

    Thanx

    Meticulous

  • #2
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up (e) Problem solution

    Hey Meticulous try this one, it will work fine, When u call this function in the onclick event pass the parameter like this "..... onclick="return ValidateField(event)" .... "
    ---------------------------------------------------
    function ValidateField(e)
    {
    //debugger
    var asciiVal;
    var returnVal = false;

    if(window.event) // If browser type is IE
    {
    asciiVal = e.keyCode;
    }
    else if(e.charCode) // If browser type is Netscape/Firefox/Opera
    {
    asciiVal= e.charCode;
    }

    // Key Code Values :
    // 32 = " "
    // 47 = "/"
    // 48 - 57 = 0 -9

    if (asciiVal > 31 && (asciiVal < 47 || asciiVal > 57))
    alert("Only numbers and '/' are allowed");
    else
    returnVal = true;

    return returnVal;
    }
    ------------------------------------------------------------------

    sundar

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Aa much better validation is to use a regex:-

    Code:
    <input type = "text" id = "txt1" size = "12" onblur = "validateField(this)">
    
    <script type = "text/javascript">
    function validateField(which) {
    fldval = which.value;
    fldval = fldval.replace(/^\s+/,"");  // strip leading spaces
    if (/[^a-z0-9\s]/i.test(fldval)) {  // only alphanumeric and space allowed
    alert ("You have entered one or more invalid characters - try again!");
    which.value = "";
    which.focus();
    return false;
    }
    }
    </script>
    If spaces are not allowed delete the \s in blue.


    Why should I do anything for posterity? What has posterity ever done for me? Groucho Marx (1890 - 1977)
    Last edited by Philip M; 08-20-2008 at 07:59 AM.

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Philip , I do agree to your post,

    The script which i posted doesn't allow the user to enter the invalid character. But in ur case the user can enter the invalid character and then when he changes the focus he gets the alert msg. Also when u use the onBlur event the control loses focus after the alert box, so user has to again click on the control to edit it, this becomes annoying. So think before using onBlur event.

    If u have a better solution as an alternative to the onBlur event plz let me know.

    Thnx... :-)

    sundar

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by m_sundar View Post
    Also when u use the onBlur event the control loses focus after the alert box, so user has to again click on the control to edit it, this becomes annoying.
    sundar
    No, that is incorrect, did you not notice:-

    alert ("You have entered one or more invalid characters - try again!");
    which.value = "";
    which.focus();

    The OP requested no special characters, not numbers only.

    If you want a script which prevents special characters from being entered in the first place or pasted in then:-

    Code:
    <script type="text/JavaScript">
    function valid(f) {
    f.value = f.value.replace(/[^a-z0-9\s]/gi,"");   // only alphanumeric and space allowed
    }
    </script>
    
    <form id="myform" action="">
    <input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
    </form>
    As always server-side checking is also required as Javascript may be disabled.

    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 08-20-2008 at 08:57 AM.

  • #6
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Philip,

    I have made a mistake , its not onclick , it is onkeypress event.. that was my mistake...oops...

    And the key code i have given over there is just for an example, the OP has to modify according to his needs,

    Also when u use onkeyup event u have to use which.value = "" , u cant avoid it bcos the invalid character will remain over there unless u clear it manually.

    thnx and regards

    Sundar :-)


  •  

    Posting Permissions

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