0

我从 Joomla 组件 Vik Rent Cars 中创建了一个自定义字段,自定义字段的 id 是“vrcf13”。我怎样才能让它只接受 8 个数字?

代码是:

<table class="vrccustomfields">
    <?php
    $currentUser = JFactory::getUser();
    $juseremail = !empty($currentUser->email) ? $currentUser->email : "";
    foreach ($cfields as $cf) {
        if (intval($cf['required']) == 1) {
            $isreq = "<span class=\"vrcrequired\"><sup>*</sup></span> ";
        } else {
            $isreq = "";
        }
        if (!empty ($cf['poplink'])) {
            $fname = "<a href=\"" . $cf['poplink'] . "\" id=\"vrcf" . $cf['id'] . "\" rel=\"{handler: 'iframe', size: {x: 750, y: 600}}\" target=\"_blank\" class=\"modal\">" . JText :: _($cf['name']) . "</a>";
            } else { 
            $fname = "<span id=\"vrcf" . $cf['id'] . "\">" . JText :: _($cf['name']) . "</span>";
                            }
        if ($cf['type'] == "text") {
            $textmailval = intval($cf['isemail']) == 1 ? $juseremail : "";
        ?>
                    <tr><td align="right"><?php echo $isreq; ?><?php echo $fname; ?> </td><td><input type="text" name="vrcf<?php echo $cf['id']; ?>" value="<?php echo $textmailval; ?>" size="40" class="vrcinput"/></td></tr>
        <?php

        }elseif ($cf['type'] == "textarea") {
        ?>
                    <tr><td valign="top" align="right"><?php echo $isreq; ?><?php echo $fname; ?> </td><td><textarea name="vrcf<?php echo $cf['id']; ?>" rows="5" cols="30" class="vrctextarea"></textarea></td></tr>
        <?php

        }elseif ($cf['type'] == "date") {
        ?>
                    <tr><td valign="top" align="right"><?php echo $isreq; ?><?php echo $fname; ?> </td><td><?php echo JHTML::_('calendar', '', 'vrcf'.$cf['id'], 'vrcf'.$cf['id'].'date', $nowdf, array('class'=>'vrcinput', 'size'=>'10',  'maxlength'=>'19')); ?></td></tr>
        <?php

        }elseif ($cf['type'] == "select") {
            $answ = explode(";;__;;", $cf['choose']);
            $wcfsel = "<select name=\"vrcf" . $cf['id'] . "\">\n";
            foreach ($answ as $aw) {
                if (!empty ($aw)) {
                    $wcfsel .= "<option value=\"" . $aw . "\">" . $aw . "</option>\n";
                }
            }
            $wcfsel .= "</select>\n";
        ?>
4

1 回答 1

0

如果我对您的理解正确,您希望防止用户在文本字段中输入任何非数字且超过 8 个字符的内容。这可以使用 Javascript 来实现。这假设您有可用的 jQuery。

$(function() {
    $("#number").keydown(function(key) {
        // This will only ensure the key pressed is a number (excludes the numpad)
        if(key.keyCode < 48 || key.keyCode > 57) {
            return false;
        }                                                         
        // Prevent more than 8 insertions
        if($(this).val().length > 8) {
            return false;
        }
    });
});

正如评论中提到的,这只会确保按下数字键并且不适合小键盘。它还会阻止用户删除等。您可以查找您希望允许的键码的完整列表。

使用 JS 的替代方法是在提交后检查值服务器端。您可以将 PHPfilter_var()FILTER_VALIDATE_INT标志一起使用strlen()

于 2013-08-22T12:59:35.703 回答