Simple Space Problem...

1. Senior Member
Join Date
Dec 1969
Posts
666

## Simple Space Problem...

I&#039;m checking for the user phone number. <BR><BR>This is my code:<BR>if (!(frm.phone.value.search (/^[a-z0-9-]{4,12}\$/) &#062; -1))<BR>{<BR>alert(&#039;Please fill in phone number&#039;);<BR>frm.phone.focus()<BR>return false;<BR>}<BR><BR>The problem with this code is that I CAN&#039;T enter spaces.<BR>How can I fix it so I can enter spaces to the field as well?<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
1,323

## RE: Simple Space Problem...

gott surround the value with single quotes &#039;value&#039;...

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## WHAT???

I don&#039;t pretend to understand that. Would you care to explain????<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Depends...

You want to allow a space *anywhere* or only in certain locations within the field&#039;s value?<BR><BR>If you want to allow a space anywhere, just add one to the list of allowed characters.<BR><BR>How come you are allowing lower case letters in a phone number? And why ONLY lower case???<BR><BR>And only 4 digits??? A 4 digit phone is okay???<BR><BR>And what about people who put (...) around the area code of a number???<BR><BR>Personally, I would get fussy and insist on a properly formatted phone number, *ONLY*:<BR><BR>var okay = /^(((ddd)&#124ddd)[ -])?ddd[ -]dddd%/<BR><BR>that allows either (805) or 805 or nothing as the area code and allows either a dash or space between parts of the number.<BR><BR>Assuming I coded it right.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
666

## I allow to enter letter for people that have exten

in their number. That&#039;s why the letters...<BR>(805) 222-3333 Ext. 3<BR><BR>I&#039;m not fimiliar with the way you&#039;re checking the phone number. Looks really interesting. How is the code working?<BR><BR>Thanks!

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not worth going into...

Since you need something more flexible, I guess I&#039;d just simply exclude any characters that I don&#039;t want to see:<BR><BR>var okay = /[^da-zA-Z() -.]/<BR><BR>if ( okay.test( fld.value ) ) <BR>{<BR> alert("An unexpected character was found in your phone number listing")<BR> return false;<BR>}<BR>if ( fld.value.length &#060; 4 ) <BR>{<BR> alert("That phone number is too short.");<BR> return false;<BR>}<BR><BR>Or something like that.<BR>

7. Senior Member
Join Date
Dec 1969
Posts
666

## Actually, I wouldl ove to know what you did there.

Can you show me a full example?<BR>What&#039;s the "d" stands for?

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Okay, explaining it...

var okay = /^(((ddd)&#124ddd)[ -])?ddd[ -]dddd\$/ <BR><BR>[I had a % as penultimate character before...just a typo.]<BR><BR>First of all, d means "one digit". Same as [0-9], just shorter to type.<BR><BR><BR>/ ::: begin regular expression<BR>^ ::: beginning of text<BR>(((ddd)&#124ddd)[ -])? ::: outermost parens delimit a group<BR> ::: the following ? makes whole group optional<BR><BR>((ddd)&#124ddd) ::: again, outermost parens delimit a group<BR> ::: the &#124 in the middle means "either...or..."<BR><BR>(ddd) ::: we need to use ( to mean we *want* a left paren character<BR> ::: (so it is not taken as a grouping character<BR> ::: so that set of characters means we will accept<BR> ::: (123) or (090) or any set of 3 digits inside of parens<BR><BR>ddd ::: just accepts 3 digits<BR><BR>So going back to:<BR>((ddd)&#124ddd) ::: accept 3 digits, either inside parens or not<BR><BR>and back to:<BR>(((ddd)&#124ddd)[ -])? ::: accept 3 digits, w/ or w/o parens, followed by optional space or dash<BR><BR>Continuing:<BR>ddd ::: again, 3 digits. This time not optional, at all.<BR><BR>[ -] ::: either space or dash, not optional <BR> ::: (but could add ? after that to make them optional)<BR><BR>dddd ::: 4 digits, not optional<BR>\$ ::: end of text<BR>/ ::: end of regular expression<BR><BR>SO:<BR><BR>Accept a phone number of the form<BR> ###-####<BR>or<BR> ### ####<BR>*optionally* preceded by an area code<BR> (###)<BR>or<BR> ###<BR>and witha separator of space or dash between area code and rest of number.<BR><BR>Passes test:<BR> (805) 555-1212<BR> 805-555-1212<BR> 805 555 1212<BR> (805)-555 1212<BR>Fails test:<BR> 805-5551212 [no space or dash after 555]<BR> #805 555 1212 [# character not legal]<BR> 805-555-123 [only 3 digits in last part of number]<BR><BR>Okay?<BR><BR><BR> <BR>

9. Senior Member
Join Date
Dec 1969
Posts
666

## Oh Cool!!!

Thanks so much, that was so helpful!

#### Posting Permissions

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