All Type Coding

Search Here

Alphanumeric validation in javascript

 function textAlphanumeric(obj, evt1) {
            var ctrl = obj.id;
            var charCode1 = (evt1.which) ? evt1.which : event.keyCode
            if ((charCode1 >= 48 && charCode1 <= 57) || (charCode1 >= 65 && charCode1 <= 90) || (charCode1 >= 97 && charCode1 <= 122) || (charCode1 == 8) || (charCode1 == 32)) {

                return true;
            }
            else {
                // alert("Please enter alpha-numeric only");
                ShowPopupMessage('Please enter alpha-numeric only', ctrl);
                //$('#' + ctrl).focus();
                //$('#' + ctrl).val('');
                //  return false;
            }

        }


We can call this function like below.

 TextBox txta -=new TextBox();
 txta is a dynamically TextBox.

  txta.Attributes.Add("onkeypress", "return textAlphanumeric(this,event)");