3

can anyone tell methods of form validation other than regex in javascript also can anyone suggest any alternate methods without Regex for this following code?

           function AllowAlphabet(){
           if (!frm.alphabet.value.match(/^[a-zA-Z]+$/) && frm.alphabet.value !="")
           {
                frm.alphabet.value="";


                alert("Please Enter only alphabets");
           }
           if(frm.alphabet.value.length > 5)
           alert("max length exceeded");
2

3 Answers 3

1

you could do:

var keyCode = window.event.keyCode || event.which;
if ((keyCode < 65 || keyCode > 90) && (keyCode < 97 || keyCode > 123) ) {
     alert("only letters allowed");
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'd do it with RegEx if I'm honest, however since you're asking, maybe you can do something like this:

var isValid = function (input) {
    for (var i = 0; i < input.length; i++) {
        var c = input.charAt(i);
        if (!isNaN(parseInt(c))) {
            return false;
        }
    }
    return true;
};

Here is a fiddle to demonstrate: http://jsfiddle.net/SVy3R/1/

I'd like to note that I'm not sure what the performance here is, also I haven't tested it for various corner cases, but it should do the trick.

Comments

0
//this function allow only alphabets in textbox

<script language="Javascript" type="text/javascript">
    function onlyAlphabets(e, t) {
        try {
            if (window.event){
                var charCode = window.event.keyCode;
            }
            else if (e) {
                var charCode = e.which;
            }
            else { return true; }

            if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
                return true;
            else
                return false;
        }
        catch (err) {
            alert(err.Description);
        }
    }
    </script>
<input type="text" onkeypress="return onlyAlphabets(event,this);" />

2 Comments

i'm a beginner so can you explain the above code? it wil be helpful
hello ashok put this script in your head section in your file and onkeypress="return onlyAlphabets(event,this);" use this function on which you want only alphabate. this function allow only alphabate and restrict pther letters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.