【问题标题】:Restrict number of input in a form when selected in a select dropdown在选择下拉列表中选择时限制表单中的输入数量
【发布时间】:2014-10-31 10:03:32
【问题描述】:

我想知道在选择选择输入时如何使用可能的 javascript/html(表单验证)来限制输入数量。场景:我首先有一个下拉框,您可以在其中选择手机号码的国家代码;一旦选择,当然必须输入他们的手机号码(手机号码的长度取决于您选择的国家代码,例如 +63[philippines] 并且用户手机号码只能是 11 个字符长)。以下是我的代码行:

 <div class="input-append">
    <select name="mobile" tabindex="15" style="height: 30px; width: 80px;" required>
        <option <?php if (isset($source) && $source=="Philippines") ?>>+63 (Philippines)</option>
        <option <?php if (isset($source) && $source=="America") ?>>+1 (America)</option>
        <option <?php if (isset($source) && $source=="UK") ?>>+44 (United Kingdom)</option>
        <option <?php if (isset($source) && $source=="Japan") ?>>+81 (Japan)</option>
        <option <?php if (isset($source) && $source=="Korea") ?>>+82 (Korea)</option>
        <option <?php if (isset($source) && $source=="China") ?>>+86 (China)</option>
    </select>
    <input type="number" name="mobile_phone" tabindex="16" style="height: 30px; width: 127px;" placeholder="Enter your Mobile Phone Number" maxlength="11"required>
 </div>

感谢您的友好回复!

【问题讨论】:

  • 使用最小和最大属性检查这个wufoo.com/html5/attributes/04-minmaxstep.html
  • 使用javascript的onchange事件。
  • 我不认为这个工具可以使用 Javascript。因此,用户 Jquery 而不是 Javascript 则明智地根据选项值编写自定义代码。请参阅此链接以获取 jquery 示例:stackoverflow.com/questions/6141680/…
  • 是这样吗?我不知道该怎么做,因为下拉框中有很多选择,这使得验证更加困难。无论如何谢谢! :)

标签: javascript php html


【解决方案1】:

你可以试试http://parsleyjs.org/doc/index.html#psly-validators-list和一些正则表达式:

 ^\+63\d{11}$ - must start with +63 followed by 11 digits

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多