rg_
rg_

Reputation: 431

jQuery conditional field validation (at least one of two fields must be filled)

http://jsfiddle.net/sarah97302/8LRt4/2/

I can't even test to see if this works in jsfiddle because I keep getting an error message when I try to submit the form:

{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x3ff9310>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0x3ff82d0>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0x3ff9310>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0x3ff8390>, 'help_text': '', 'name': 'js_wrap'}"}

I have looked around and based on other questions it looks like the error message means I need to disable the form submit in some way but I'm not sure how to do that and still test the validation function?

I want an alert message to show up if I try to submit without at least one of the two fields not filled (Date of birth or PIN).

function Validate() {
if ( $( "#custom-105959", "#custom-105976").val() === "") 
  {
alert( "You must enter either your date of birth or your Personal Identification Number (PIN) ." );
$( "#custom-105959").focus();
      event.preventDefault(); }

 }

$('#signup').submit(Validate);

and html:

<form id="signup" method="post" action="">
<div><label >Name</label>
        <input  id="custom-105958"  name="custom-105958" type="text"></div>
    <div><label >Birthday (mm/dd/yy)</label>
        <input  id="custom-105959"  name="custom-105959" type="text"></div>

    <div><label >Personal Identification Number (PIN)</label>
        <input  id="custom-105976" name="custom-105976" type="text">  </div>    
    <input type="submit" value="Submit" />


</form>

Upvotes: 0

Views: 583

Answers (1)

falcon
falcon

Reputation: 357

EDIT:

function Validate() {
    var customfirst = document.getElementById('custom-105959').value;
    var customsecond = document.getElementById('custom-105976').value;
    if (customfirst == '' && customsecond == ''){
        alert('empty');   
    }
    else {
        alert('inserted');   
    }
}

http://jsfiddle.net/8LRt4/4/

Upvotes: 1

Related Questions