SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Data Validation

  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Data Validation

    I'm a newbie to JavaScript and I am creating a form with a input field which needs validating. The field must contain two capital letters at the beginning and then a string of numbers any length and no spaces. i.e. "AB12345". I am able to validate fields with just numbers or letters but cannot fathom out how to do it with a combination of both.

    Any help would be much appriciated.

    AndyLil

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I can answer this one

    Ok, what you need is the pattern matching powers of regular expressions (they've been quite popular here lately, search this forum for 'regular expression' and you'll see what I mean) Anyhoo. Regular expressions are specially formatted strings for pattern matching (& more, but we'll focus on the matching part now.

    Ok, here's a quickie form
    Code:
    <form name="form1" onSubmit="return validateForm(this)">
    	<input type="text" name="code" /><br />
    	<input type="submit" name="Submit" value="Submit" />
    </form>
    Now, in the <HEAD> of the page, we need to add some javascript, like this:
    Code:
    <script language="javascript">
    function validateForm(Frm)
    	{
    	var formObj = Frm.code;
    	
    	var codeReg = new RegExp(/^[A-Z]{2}\d*$/)
    	
    	if (!codeReg.test(formObj.value))
    		{
    		alert("Your data is not valid for the "+formObj.name);
    		formObj.select();
    		formObj.focus();
    		return false;
    		}
    	return true;
    	}
    </script>
    Ok, so when the user submits the form the function validateForm() runs first, and returns either a TRUE or FALSE value.

    Instead of exlpaining the ins and outs of regex's to you, just visit this page, as it's quite a lengthy topic

    http://developer.netscape.com/docs/m...ide/regexp.htm (Thanks to Flawless_koder for the link)

    Also, please visit my page that has a great (IMHO ) javascript form validator.
    http://www.peterbailey.net/jsdemo
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use \d+ - you want one or more occurances - right?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ya.... \d+ would be better....

    or maybe require a minimum of 3 or soemthing
    \d{3,}
    Last edited by beetle; Jul 26, 2002 at 05:23.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks for your help guys that worked a treat. I'll get my head stuck into RegExes now.

    Thanks again

    AndyLil

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good idea - you won't regret it!

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •