Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have to validate the textbox to enter only alpha numeric characters.
The function validateAlphaNumeric(evt, txtbox) will fire onkeypress event on textbox.
Below is the function written in javascript.

But i am not able to get the value of the textbox if i do Ctrl+V. i need to validate if user pastes.
Can any one suggest me on this?

C#
function validateAlphaNumeric(evt, textBox) {
        /*  File Description    :   Numbers,Characters,Hyphen(-),Slash(/)and Space */
        var charCode;   

        charCode = (evt.which) ? evt.which : window.event.keyCode;

        if (charCode >= 97 && charCode <= 122 || charCode >= 65 && charCode <= 90 || charCode == 8 || charCode >= 48 && charCode <= 57 || charCode == 45) {
            return true;
        }
        else {
            var errorMsg = document.getElementById(textBox.id + 'Error');
            if (errorMsg != null) {
                errorMsg.innerText = "Please Enter Alpha – Numeric Characters only";
            }
            return false;
        }        
    }

Thanks in advance
Posted
Updated 21-May-14 20:08pm
v2
Comments
Ajith K Gatty 22-May-14 2:18am    
Hello..If you want alphanemerics only, why dont you use /\w+/???
Member 9907886 22-May-14 3:10am    
Yup...But if I paste some special characters instead of typing then i dont get the value of the textbox....Instead it shows Null.

You can read this article - Javascript Key Event Test Script[^]. Get the values of Ctrl + V.
 
Share this answer
 
Comments
Member 9907886 22-May-14 2:31am    
Thanks for the reply...
but i didnt get where to write the code?"textInput Data"
How to get this value?

Thanks,
restrict to paste in textbox .

<asp:textbox id="txtrepass" runat="server" cssclass="txtbx2" width="164px" textmode="Password" oncopy="return false" onpaste="return false" oncut="return false" onkeydown="return DisableCopyPaste(event)" onmousedown="return DisableCopyPaste (event)" >
 
Share this answer
 
v2
Comments
Member 9907886 22-May-14 2:44am    
Hi Manvendra,
I need Copy and Paste... I should not disable that...
But after paste operation i need to validate that value.

Thanks....
Bh@gyesh 22-May-14 3:03am    
try to call validate event on "onpaste" event instead of "return false"
Member 9907886 22-May-14 4:34am    
Hmmm .... i got another option to get the value...
event.currentTarget.value - u will get the value of the control...
Based on this I may need to change the code...

Thanks,
In My Code I Had this ...
So It Might Help You..
..
C#
$(document).ready(function()
{
    var ctrlDown = false;
    var ctrlKey = 17, vKey = 86, cKey = 67;

    $(document).keydown(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = false;
    });

    $(".no-copy-paste").keydown(function(e)
    {
        if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
    });
});

..
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900