Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>validate string

validate string

nikko50
03-15-2006, 11:14 PM
Hey all.
I would like to validate a string but am confused how I can do it. The string must be look like this...
xxxxx-xxxxxx

So there must be 6 charactors infront and after a hyphen. How can I do this??
Tracy

Beagle
03-15-2006, 11:21 PM
use regular expressions:

var reg = '/^.{6}-.{6}$/'

if (str.match(reg))
{
alert('Valid');
}

Philip M
03-16-2006, 08:29 AM
use regular expressions:

var reg = '/^.{6}-.{6}$/'

if (str.match(reg))
{
alert('Valid');
}


Possibly not what Nikko wants. A . (dot) matches any character including a space or a special character such as a bracket.

More likely what is wanted is
var reg = '/^[a-zA-Z]{6}-[a-zA-Z]{6}$/'

i.e. 6 alpha charcaters hyphen 6 alpha characters

Kor
03-16-2006, 11:07 AM
You may use a shorthand A-z for alpha. No need of quotes

for alpha
var reg = /^[A-z]{6}-[A-z]{6}$/;
for numbers
var reg = /^\d{6}-\d{6}$/;
for numbers and alpha (but no special characers)
var reg = /^[A-z-\d]{6}-[A-z-\d]{6}$/;


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

HP sharpens blade PC l
Hyperthreading hurts s
can not call click() i
script does not work o
smart menu 
write to an html file 
FireFox doesn't recogn
Changing e.keyCode (e.
Clearing a range creat
UPDATE: My mouse over 

News Archive

onChange Help 
beforeload? vs onload.
Problem with document.
decreasing my javascri
Problem with \\ and \ 
how to stop scrolling?
Change Label Text Colo
Can I forward a page b
2 javascript functions
Validating a textbox 

Related stories:

Mozilla + Mediplayer + Javascript
Looking for a script that scrolls
Problem in Script
Stock Data
Restrict Scrolling
Make the only clickable area the center of the screen
Am I missing something, Validating?
Having trouble with some javascript code...
Script for date validation...

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics