Results 1 to 5 of 5

Thread: Javascript validation for multiple text fields with identical names

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Javascript validation for multiple text fields with identical names

    Hello All,

    I have a form that has several textboxes with identical names. I figured out a way to validate them by going row by row in the DOM. The problem is that I have a checkbox associated with each row and I need to validate only those rows that are checked and ignore the ones that aren't. Any ideas on how this might be accomplished? thanks,

    Here is my validating procedure:


    function isValid(){
    var isDecimal_re = /^\d+(\.\d+)+$/; //to check for decimal input
    var texts=document.getElementsByTagName("input");

    columns=4;
    for (i=0; i<texts.length; i++)
    {
    if (texts[i].type=="text")
    {
    if (!isDecimal_re.test(texts[i].value))
    {
    columnId=(i%columns)+1;
    rowId=Math.ceil(i/columns)+1;
    switch(columnId)
    {
    case 1:
    alert("Invalid Number, has to be decimal");
    texts[i].focus();
    return false;

    case 2:
    alert("Invalid Number, has to be decimal");
    texts[i].focus();
    return false;

    case 3:
    alert("Invalid Number, has to be decimal");
    texts[i].focus();
    return false;

    case 4:
    alert("Invalid Number, has to be decimal");
    texts[i].focus();
    return false;
    }
    }

    }

    }
    }


    I have a checkbox named counter associated with each row and I need to validate only those rows where counter is checked. I tried using the getElementById for the checkbox and see if it is checked but I'm having trouble determining where the end of the row is for that particular checkbox!

  2. #2
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    Are the inputs and check boxes dynamically generated? can't you name them like mytest1 mytext2 etc and the checkboxes like chk1 chk2 etc?

  3. #3
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    Yes the input boxes and check boxes names are dynamically generated. I have a textbox_#currentrow# that assigns a unique name to each of these as well as the checkboxes. I'm a noob to Javascript and don't know how I can reference them in the document. thanks,

  4. #4
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    i don't know the simple code for this validating, but i find a plug-in that you may be need:

    try open this one
    http://www.webresourcesdepot.com/inl...n-with-jquery/

    you can use the same name for your textbox, with different alert. But you need to learn the javscript, how it's work.
    good luck

  5. #5
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks davelf,

    Although I'm even more of a noob with jquery than js, I'll give it a shot and see if I can figure a way out to customize it for my app. without spending too much time on it. thanks again,

    roger

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •