0

我有一个简短的表格,需要一些验证帮助。当前的 javascript 要求完成每个字段...除了电话号码。但是,如果有人选择联系进行咨询,我希望电话字段是必需的。希望我目前的工作不必被丢弃。任何帮助都会很棒。谢谢!

Javascript

<script type="text/javascript"> 

function validateForm(f)
{
    // If they entered an email address, then edit it before submitting the form

    // Require Email Address
    if (f. field_45042.value == '')
    {
        alert('Please enter your email address.')
        f. field_45042.focus()
        return false;
    } 

    // Require First Name
    if (f. field_57841.value == '')
    {
        alert('Please enter your first name.')
        f. field_57841.focus()
        return false;
    } 

    // Require Last Name
    if (f. field_57842.value == '')
    {
        alert('Please enter your last name.')
        f. field_57842.focus()
        return false;
    } 

    // Require Zip Code
    if (f. field_70761.value == '')
    {
        alert('Please enter your zip code.')
        f. field_70761.focus()
        return false;
    } 

    // Require Location
    if (f.field_70694[0].selected == true)
    {
        alert('Please select a store location.')
        f.field_70694.focus()
        return false;
    } 
}

// -->
</script> 

HTML

Email Address:<input type="text" id="field_45042" class="text_field" size="35" name="45042" value="" />
First Name:<input type="text" id="field_57841" class="text_field" size="35" name="45043[57841]" value="" />
Last Name:<input type="text" id="field_57842" class="text_field" size="35" name="45044[57842]" value="" />
Zip Code:<input type="text" id="field_70761" class="text_field" size="35" name="45045[70761]" value="" />
Telephone:<input type="text" id="field_62646" class="text_field" size="35" name="45046[62646]" value="" />

Preferred Store Location:
<select id="field_70694" class="select field" name="42714[70694]" >
    <option value="Select One of the Following:" >Select one of the following:</option>
    <option value="Store 1" >Store 1</option>
    <option value="Store 1" >Store 2</option>
    <option value="Store 1" >Store 3</option>
</select>

Please contact me for a FREE consultation.
<select id="field_71849" class="select field" name="45924[71849]" >
    <option value="Yes">Yes</option>
    <option value="No" >No</option>
</select>
4

2 回答 2

1

这应该有效:

if (f.field_71849.value == "Yes" && f.field_62646.value == '') {

    alert('Please enter a phone number.')
    f.field_62646.focus()
    return false;
} 
于 2012-09-22T03:16:39.937 回答
1
 // ...

 // require phone if wants to be contacted
 if(f.field_71849.value == 'Yes' && f.field_62646.value == ''){
   alert('Please enter your phone number');
   return false;
 }

 // ...
于 2012-09-22T03:21:46.430 回答