使用正则表达式验证

时间:2013-05-06 09:23:30

标签: php javascript regex validation

我正在设置付款页面,我是设计师,而不是开发人员。所以我遇到了一些问题。付款公司给了我一些方向,但是他们包括使用正常表达式为'AMOUNT'字段进行验证。我已经研究过“正则表达式”但是没有成功地为他们提供的“checkfor”添加更多内容。我需要简单的验证,只需要三个必填字段,'名称',CUST_NUM和'条款'(这是一个复选框)。任何人都可以启发我吗?

<form action=http://rcehholidaytrust.com/account/redirectoffresponsepage.php method=post onSubmit="return checkform()">
    <table width="400" border="0" cellspacing="1" cellpadding="0">
        <tr>
            <td><div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Name </font></div></td>
            <td><input type="text" name="name" size="48" /></td>
        </tr>
        <tr>
            <td><div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Acc. No.</font></td>
            <td><label for="client"></label>
            <input name="CUST_NUM" type="text" id="client" size="48" /></td>
        </tr>
        <tr>
            <td><div align="right">Payment €</div></td>
            <td><label for="AMOUNT"></label>
            <input name="AMOUNT" type="text" id="AMOUNT" size="47" /></td>
        </tr>
        <tr>
            <td><div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Address</font></div></td>
            <td><textarea name="address" wrap="virtual" cols="35" rows="5"></textarea></td>
        </tr>
        <tr>
            <td><div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Telephone</font></div></td>
            <td><input type="text" name="telephone" size="48" /></td>
        </tr>
        <tr>
            <td><p align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Email</font></p></td>
            <td><input type="text" name="email" size="48" /></td>
        </tr>
        <tr>
            <td><div align="right"></div></td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="checkbox" name="terms" id="terms" />
            <label for="terms">Agree to terms and conditions</label></td>
        </tr>
    </table>
    <input type=submit value="Proceed to secure server">
</form>


<script>
function checkform()
{
    var amount = document.getElementById("AMOUNT").value;
    if(amount.match(/^[0-9]{1,6}\.[0-9]{2}$/))
    {
        return true;
    }
    // else if(amount.match(/^[0-9]{1,2,3,4,5,6,7,8}$/))
    //{
    //                return true;
    //}
    else
    {
        alert("Please enter a valid amount i.e. 12.34EUR = 12.34");
        return false;
    }
}

</script>

2 个答案:

答案 0 :(得分:0)

尝试使用test解决您的问题:

适用于所有验证

function checkform()
{
    var reg=/^[0-9]{1,6}\.[0-9]{2}$/;

    var status=true;
    if(!reg.test(document.getElementById('AMOUNT').value) && document.getElementById('AMOUNT').indexOf('.')==-1 )
    // adding 1 more ending brace
    {
       status=false;
    }
    if(document.getElementById('client').value=="" || document.getElementById('name').value=="")
    // change here client in place of CUST_NAME
    {
        status=false;
    }
    if(document.getElementById('terms').checked==false)
    {
        status=false;
    }
    if(status==false)
       alert("Please check all fields");
    return status;
} 

小提琴 http://jsfiddle.net/zUQK9/

答案 1 :(得分:0)

function checkform()
{
    var checks=0;
    var amount = document.getElementById("AMOUNT").value;
    if(amount.match(/^[0-9]{1,6}\.[0-9]{2}$/))
    {
        checks++;
    }

    var name = document.getElementById("name").value;
    if(name != '') //if you want to check if it's not empty, you could also use: name.match(/^[\w\s\d]+$/) to check if name contains any combination of numbers, chars and spaces.
    {
        checks++;
    }

    if(document.getElementById("terms").checked) 
    {
        checks++;
    }

    if(checks==3){return true;}
    alert("Please enter a valid amount i.e. 12.34EUR = 12.34");
    return false;
}

这应该有效,没有测试它所以告诉我是否有什么不起作用!