Skip to main content
Inspiring
August 29, 2007
Question

JS validation for dynamic textbox

  • August 29, 2007
  • 2 replies
  • 1049 views
How can i validate dynamic textbox using javascript?.
item_val_#i# is required field.


<cfloop from="1" to="#count#" index="i">
<input type="text" name="item_val_#i#" size="5" maxlength="5">
</cfloop>
    This topic has been closed for replies.

    2 replies

    Inspiring
    August 30, 2007
    use the same cfloop inside your js code (assuming it is IN THE SAME
    PAGE, not in a separate file).
    obviously, your #count# variable must be set BEFORE the js code...

    something like [not complete function]:

    function validatefields(){
    var errstr = "";
    <cfloop...>
    (document.forms[0].elements["item_val_#i#"].value=="") ? errstr =
    errstr + "\nField Myfield#i# must have a value!" : errstr = errstr;
    </cfloop>
    .....
    }

    but i would suggest you better validate input server-side...

    --

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    Inspiring
    August 29, 2007
    The easiest way is to use cfform and cfinput. Then you can use the required and message attributes for cfinput.
    JavageneAuthor
    Inspiring
    August 31, 2007
    cfinput required="yes" is not working...

    anybody validation for dynamic elements?.
    JavageneAuthor
    Inspiring
    August 31, 2007
    I am using modified code from this page....
    http://tutorial362.easycfm.com/


    I want to validate one textbox, if textbox is present....

    i am not able reference textbox by name, textbox name changes in every row. Name is dynamic.
    Textbox name changes like this....

    item_val_1
    item_val_2
    item_val_3
    .
    .
    item_val_7
    .
    .