0

I am a designer and I am not very familiar with regular expressions. I have made quite an effort to get this far with some help from some very kind posters to this forum. The script below called checkform should validate three fields, 'client', 'terms' and 'AMOUNT' . It is quite straightforward, client and terms are required fields, while 'AMOUNT' needs to be inputted with proper decimal formatting eg. € 27.00. It validates 'client' and 'terms but completely ignores 'AMOUNT' ? Does not check it for anything.

Any help would be very much appreciated.

<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>
        <p>&nbsp;</p>

    <input type=submit value="Proceed to secure server">
</form>


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

        var status=true;
     if(!reg.test(document.getElementById('AMOUNT').value))
     {
        status=false;
     }
     if(document.getElementById('client').value=="" || document.getElementById('name').value=="")
     {
            status=false;
     }
     if(document.getElementById('terms').checked==false)
        {
            status=false;
     }
        if(status==false)
        alert("Please check all fields");
        return status;
    }    
</script>
</body>
</html>
andrewsi
  • 10,807
  • 132
  • 35
  • 51
paddym
  • 29
  • 1
  • 7

3 Answers3

2

Your regex ^[0-9]{1,6}\.[0-9]{2}$ would match 1 to 6 digits followed by 2 decimal digits.


You can try this

^[0-9]{1,6}(\.[0-9]{2})?$
//This would match  1 to 6 digits optionally followed by 2 decimal digits.

OR

 ^[0-9]{1,6}(\.[0-9]{1,2})?$
//This would match  1 to 6 digits optionally followed by 1 to 2 decimal digits.

? matches preceding group or character optionally


EDIT

You are using the same name for id and name attribute in some cases and in some cases you are using 1 of them .Have a look at this and this

 <input type="text" name="name" size="48" />

should be

<input name="name" type="text" name="name" id="name" size="48" />
Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • I need it to match 1 to 6 digits followed by 2 decimal digits, my problem with the script is that it does not work, it does not ask for the decimal point or any validation for the 'AMOUNT' See http://rcehholidaytrust.com/account/request-stack.php – paddym May 06 '13 at 14:32
  • Anirudh, you are right, I got a similar response from Barmar. Thanks very much. I will also have a read of that webpage. I did not know this.Thanks for taking time out of your day to help me, it is very much appreciated. – paddym May 06 '13 at 15:12
1

The regular expression is working fine. The script is failing at

document.getElementById('name').value==""

There's no element with ID name, so this gets an error. You need to add an id attribute to the name input:

<input type="text" name="name" id="name" size="48" />

To give a different alert for the AMOUNT field, put the alert inside the block that handles failing the regexp test.

 if(!reg.test(document.getElementById('AMOUNT').value))
 {
    alert("Please enter a valid amount");
    return false;
 }

Use return false here so you don't get a second alert when it gets to the end of the function.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Barmar, thanks for getting back to me. I am afraid I do not understand regular expressions or javascript so I am not sure how to run the console or debugger. How should this line read?document.getElementById('client').value=="" – paddym May 06 '13 at 14:35
  • Made a mistake, it's the `name` ID that's missing. I've updated the answer. You run the debugger by opening `Developer Tools` in IE or Chrome, or the Web Inspector in Safari, or the Web Console or Firebug in Firefox. Don't you think you should learn how to use your tools if you're going to be programming in Javascript? – Barmar May 06 '13 at 14:47
  • Barmar, that fixed it! Thanks for taking the time. Do you know if it would be difficult to have a different alert for the amount field. "please enter a decimal point" . Now that the validation is working, I am afraid to 'rock the cart' in case it comes undone.Thanks for taking time out of your day to help me, it is very much appreciated. – paddym May 06 '13 at 15:16
  • Put the alert inside the `if (!reg.test...)` block. – Barmar May 06 '13 at 15:17
  • Thanks Barmar, I put it in, but getting a syntax error.I cannot get the code right?`code` if(!reg.test(document.getElementById('AMOUNT').value)) { status=false; } if(status==false) alert("Please enter a valid amount i.e. 12.34EUR = 12.34"); return status; } – paddym May 06 '13 at 15:35
0

This reg ex works for me if you want to check € 27.00 with sysmbol then change regex with var reg=/^\€\s[0-9]{1,6}\.[0-9]{2}$/;;

Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12
  • The currency symbol is in the HTML next to the input field, it's not in the user's entry. – Barmar May 06 '13 at 14:17
  • Dharmesh, thanks for answering my post. I should not have referred to the 'euro' symblol as it confuses my real issue, my problem with the script is that it does not work, it requires only validation for the 'client' and 'terms' but it does not ask for the decimal point or any validation for the 'AMOUNT' See rcehholidaytrust.com/account/request-stack.php – paddym May 06 '13 at 14:38