Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q816 How can I validate a form field so that it is a fixed size (say 6) and only contains alpha numeric characters?

irt.org | Knowledge Base | JavaScript | Form 5 | Q816 [ previous next ]

Q816 How can I validate a form field so that it is a fixed size (say 6) and only contains alpha numeric characters?

Try:

<SCRIPT LANGUAGE="JavaScript"><!--
function validate(string,length) {
    if (string.length > length) {
        alert('too long');
        return false;
    }
    if (string.length < length) {
        alert('too short');
        return false;
    }

    var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    for (var i=0; i<length; i++) {
        if (valid.indexOf(string.charAt(i)) < 0) {
            alert('invalid characters');
            return false;
        }
    }

    return true;
} 
//--></SCRIPT>

Feedback on 'Q816 How can I validate a form field so that it is a fixed size (say 6) and only contains alpha numeric characters?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.