SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    textfield validation with for loop

    hi guys

    well i have one form, which having somewhere around 60 checkboxes and 28 textfields + 3 text areas, submit button

    textfields start with name="textfield1" till "textfield28"

    like

    <input name="textfield1" type="text">
    .........
    <input name="textfield28" type="text">

    now i have made one for loop to check value shouble be numeric.

    my for loop

    for (i=0; i<28;i++)
    {
    if(checknumber(document.form1.textfield[i].value)==false)
    {
    alert ("Please Insert Numerics only");
    return false;
    }
    }


    checknumber is another function which checks for numerics..

    my problem is, i m not able to increment the textfield1..28 with for loop.

    this for loop should work for the bunch of that 28 textfields only.. it should not take any other element in form.

    can anybody help me out for this

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var oForm = document.forms["myForm"];
    
    for(var i=0; i<oForm.elements.length; i++)
    {
    	if(oForm.elements[i].type == "text" && !checkNumber(oForm.elements[i].value))
    	{
    		alert("Numbers only bitte");
    		break;
    	}
    }
    Or a faster iteration (and it also makes it easier to read the code):
    Code:
    var oForm = document.forms["myForm"];
    
    for(var i=0, oField; oField = oForm.elements[i]; i++)
    {
    	if(oField.type == "text" && !checkNumber(oField.value))
    	{
    		alert("Numbers only bitte");
    		break;
    	}
    }

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi..
    thanks for the reply.. now one more change happened to the form.

    my company has introduced 14 new text field to the form.. with that now validation with form is again trouble..

    can it possible to take stack of that textfields in for loop?

    <input name="textfield1" type="text">
    .........
    <input name="textfield28" type="text">

    for loop should check only this stack.. from textfield1 to textfield28..

    please help me out

    thanks in advance..

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please help

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only want to check the first 28 fields as numeric, then only iterate the first 28 items
    Code:
    for(var i=0; i<28; i++) 
    {
        // validation magic goes here
    }


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
  •