a problem with text field verification

Discussion in 'Javascript' started by gunawardana, Dec 23, 2003.

  1. gunawardana

    gunawardana Guest

    I have to write a program to verify text field in HTML forms.
    So,I hane to verify a text field with lenth 10 & maxlenth 10.The
    entered text should be as follows.
    where xxxxxxxxx denotes a combination of numbers and y should be one
    of 'X','x','V'or 'v'.Also inputs such as 000000000v,000000000X are not
    gunawardana, Dec 23, 2003
    1. Advertisements

  2. gunawardana

    lallous Guest


    Validate the string as:

    var pat = new RegExp(/[0-9]{9}[xv]/i);
    if (pat.exec(str)==null)
    alert('Invalid input!');
    lallous, Dec 23, 2003
    1. Advertisements

  3. gunawardana

    McKirahan Guest

    I'm sure there's a Regular expression that does what you want with less
    coding but here's one solution; watch for word-wrap.

    <script language="javascript" type="text/javascript">
    function check() {
    var form = document.forms[0];
    var data = form.Data.value;
    if (data.length != 10) return;
    if (data.substr(0,9) == "000000000") return;
    for (var i=0; i<9; i++) {
    if (data.charAt(i) < "0" || data.charAt(i) > "9") return;
    if ("XxVv".indexOf(data.charAt(9)) < 0) return;
    <input type="text" name="Data" size="10" maxlength="10">
    <input type="button" value="Check" onclick="check()">
    McKirahan, Dec 23, 2003
  4. JRS: In article <>, seen
    But what do you mean by "such as"? With leading zero? With all zeroes?

    There is an "or" facility in a RegExp, but not AFAIK an equivalent

    Don't use a RegExp; use two, the second to deal with whatever "such as"

    OK = /^\d{9}(v|x)$/i.test(S) && /[1-9]/.test(S) // not 000000000
    OK = /^\[1-9]d{8}(v|x)$/i.test(S) // not leading zero

    See in <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
    Dr John Stockton, Dec 23, 2003
  5. Not directly. There could be, since there is nothing in the technology
    used that prohibits and "and" (and regular languages are closed under
    The closest you get is positive lookahead, i.e., to match five digits
    and at least one 4, you can write
    Agreed. Often, a very complex regular expressin can be written as
    two simple ones.

    Example: String contains n "a"'s and m "b"'s:

    Two regexps:

    I won't even begin to write a regexp for n and m with values much over 2.
    Try :)

    Lasse Reichstein Nielsen, Dec 24, 2003
  6. gunawardana

    Eric Bohlman Guest

    Actually you can use positive lookahead to implement an arbitrary "and":
    /^(?=.*this)(?=.*that)/ (a trick introduced in the _Perl Cookbook_ and
    implemented in a Perl module of mine).

    However, doing two separate tests will usually be more efficient and the
    lookahead trick should probably be used only when the match parameters
    aren't known until runtime.
    Eric Bohlman, Dec 24, 2003
  7. The problem is that you can only do this efficiently at the end of a string.
    Compare this for "or':
    If we had the hypothetical & operator, and wrote
    then we wanted the part between "z" and "cd" to contain both "this"
    and "that".

    If you do that with lookahead, you need to be able to bound the search
    somehow, or the lookahead can test past the cd. As your example:
    would incorrectly match
    "z that cd this"

    You need to ensure that the lookahead is only tested against the same
    string as the other argument to "and".

    You can do "the trick" and duplicate the continuation:
    but even that can be broken by using more complex expressions. Take
    "all digits, and at least three 4's":

    Doing the trick here gives
    However, that also matches

    Again, you have to build your RegExps so the lookahead is bounded,
    something that was not necessary with the hypothetical "&" operator.

    Lasse Reichstein Nielsen, Dec 24, 2003
  8. No. Either

    var pat = /\d{9}[xv]/i;


    var pat = new RegExp("\\d{9}[xv]", "i");
    if (! pat.test(str))
    Please do not do this, you are wasting
    scarce and thus precious resources.

    Thomas 'PointedEars' Lahn, Dec 28, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.