Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2006, 12:23 PM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regex help needed for form validation

Hello

I was wondering if anyone could help me with the following problem, and if possible, kind of explain what it means too because Javascript is all new to me! I started reading a tutorial on Regexes but then got totally confused!

I have a text box (called 'cost') in a form which needs some validation done on it
1. Although the user is entering in a cost, the value entered must be a whole number ONLY, between 100 and 100000.
2. The user could enter in a ',' or a '£' (i.e. £2,000) but these characters would be ignored.

At the moment my form validation looks like this:

<script language="javascript">

function ProcessForm()
{

if (document.forms['goalform'].elements['cost'].value == "")
    {
        alert("Please enter how much your goal will cost.");
        document.forms['goalform'].elements['cost'].focus();
        return false;
    }

    return true;

}
</script>

I need to incorporate the regex bit into this code so that my cost textfield is checked for the above mentioned validation.

If anyone is able to help, it would be greatly appreciated.

Thanks in advance

Lucy xx
 
Old January 31st, 2006, 01:21 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Lucy

I think this should work. Just pass the value of the form field into uStr:

<script language ="javascript">

function processForm(uStr)
{


    uStr = uStr.replace(/\D+/g, '')//replace everything that isn't a number with nothing.
    document.forms['goalform'].elements['cost'].value = uStr


if (document.forms['goalform'].elements['cost'].value == "")
    {
        alert("Please enter how much your goal will cost.");
        document.forms['goalform'].elements['cost'].focus();
        return false;
    }


if (document.forms['goalform'].elements['cost'].value < 100 || document.forms['goalform'].elements['cost'].value > 100000)
    {
    alert("Should be at least 100 or at the most 100000!")
    document.forms['goalform'].reset()
    document.forms['goalform'].elements['cost'].focus();
    return false;
    }


}
</script>

HTH!
Cheers
Joe
 
Old February 1st, 2006, 10:47 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe

Thanks so much for helping me out - it all works beautifully :)

Lucy xx






Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Help needed with Regex reach_reema General .NET 1 May 20th, 2008 02:46 AM
OOP:FormCreation\Validation -Paradigm shift needed webnerd PHP How-To 2 May 9th, 2005 10:03 PM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM
Values of one form needed for validation on anothe nazneen JSP Basics 0 June 19th, 2003 03:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.