Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Validate multipleHow should i check null fields for multiuple input field elements ??

    Code:
     
                                    <tr>
    					<td width="250">Id</td>
    					<td width="500">Name</td>
    					<td width="300">City</td>
    					<td width="400"><input type="button" value="Add" onclick="addRow();" /></td>
    				</tr>
                                 <tr id="r<%=i%>">
    			                 <td width="250"><input type="text" size="5" name="sno<%=i%>" value="<%=data.getSno()%>" /></td>
    					<td width="500"><input type="text" size="15" name="name<%=i%>" value="<%=data.getName() %>"/></td>
    					<td width="300"><input type="text" size="10" name="city<%=i%>" value="<%=data.getCity() %>" /></td>
    					<td width="400"><input type="button" value="Delete" id="<%=i%>" onclick="delRow(this);" /></td>
    					<td><input type="hidden" name="num" value="<%=i%>" /></td> 
    				</tr>
    Here i'm getting rows from java page and i have written a function to add rows dynamically..How can i check input fields for NULLS ?? 'id' attribute should not be used in <input> elements(f or my purpose )...How can i write validations for these input fields ?? like for example
    Code:
      if(document.forms[0].sno.value==""){
     alert('blah blah');
     return false;
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    An input field can never be null. Only "" (blank).

    Go through the fields on submission of the form and validate that all are filled in sensibly.

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe. Numeric values, such as zip codes, phone numbers and dates, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    Remember that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling Javascript.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 04-07-2014 at 12:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    abhiboppudi (04-11-2014)

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for ur advice. And my requirement here is to check fields whose field names changes dynamically (Like field1,field2,field3) when i add and delete multiple fields...So how should i do that ?? Writing document.forms[0].( fieldName + i ).value (i'm writing this in a loop with exact input field count) resulting an Error "Field not found" .

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Use square bracket notation.

    document.forms[0]["fieldName"+i].value;

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    abhiboppudi (04-12-2014)


  •  

    Posting Permissions

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