du20150401 2009-01-13 15:15
浏览 12
已采纳

验证一个或另一个

I've created a javascript function that allows me to validate if one field or the other is filled (called, shockingly enough, oneortheother). Essentially, it checks if neither is filled, or both, and throws an error.

One of the fields being validated is a input type="file" field. I'm displaying below the field the existing file, so that the users can see if it's the file they want.

Is there any way to still validate via oneortheother without having a value in the input type="file"? Any kind of javascript trickery?

I'm at wits end at this point, and have a demo later today that needs this functionality, so any help would be greatly appreciated.

EDIT:

As requested, here's some examples:

<label for="pdf">Upload PDF:
<span class="fieldnote">Files of type .pdf</span>
</label>
<input type="file" name="pdf" id="pdf" class="external_form_field oneortheother_url" value="/downloads/white_papers/HigherOrderPerl.pdf"  />
<label>Existing file:</label><span class="preview"><a href="/downloads/white_papers/HigherOrderPerl.pdf" target="_blank">HigherOrderPerl.pdf</a></span>
<label for="url">Link to asset:</label>
<input type="text" name="url" id="url" class="external_form_field oneortheother_pdf" value=""  size="25" />

Notice that the class oneortheother_url and oneortheother_pdf are applied. This allows the validation routine to know which field to compare to. The comparison is:

if (fObj.value && fObj2.value) { }

and

if (!fObj.value && !fObj2.value) { }
  • 写回答

5条回答 默认 最新

  • duanmeng3573 2009-01-13 15:37
    关注

    I am not sure how your validation function works, but it seems like you could add a hidden input that holds the url of the uploaded preview file OR the value in the file input. Then you could just compare the url input with the hidden input.

    But it seems likely you'll just need to write a custom validation function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题