SageFix. LetsFix!  
Current Location : Home > Web Development > JavaScript > Data Validation Share/Save/Bookmark    

Main Menu
SageFix - Let's Fix

Board Categories
    Web Site Designing
- Graphics
    Web Development
- .NET
- Classic ASP
- JavaScript
- PHP
    WebSite Management
- Promotion Techniques
- Search Engine Optimization
    Databases
- MySQL

Tutorials

Database Connection Strings

  Contact Us


 1 - 6 of 6 [Total 1 Pages]  
Data Validation      Jul 25, 2002, 15:49 1
  
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

 
      Jul 25, 2002, 16:13 2
  
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

 
      Jul 26, 2002, 02:07 3
  
I'd use /d+ - you want one or more occurances - right?

Flawless

 
      Jul 26, 2002, 06:30 4
  
Oh ya.... /d+ would be better....

or maybe require a minimum of 3 or soemthing
/d{3,}

 
      Jul 26, 2002, 10:47 5
  
Thanks for your help guys that worked a treat. I'll get my head stuck into RegExes now.

Thanks again

AndyLil

 
      Jul 26, 2002, 10:56 6
  
Good idea - you won't regret it!

Flawless

 
 1 - 6 of 6 [Total 1 Pages]  









Subscribe to our mailing list
email


Current Location : Home > Web Development > JavaScript > Data Validation Share/Save/Bookmark    

Development problem? SageFix is completely free -- paid for by advertisers and donations. Click here to Contact Us If you have any query. Enjoy!
Request processed in 7.0E-5 seconds Home - Contact Us - Terms Of Service - Privacy Policy - Copyrights - Top
Advertisements do not imply our endorsement of that product or service.
Current server time now is 11-Mar-2010 23:10:06
Copyright © 2009 Sagefix Inc. All rights reserved.
Powered By SageFix