?
Solved

javascript only a-z A-Z and while space

Posted on 2006-10-24
5
Medium Priority
?
331 Views
Last Modified: 2008-02-26
Hello,

I'm writing a javascript that will validate a form input. only a-z A-Z and while space are
allowed.

var passwdPat=/[^a-zA-Z]\s/g; seems not working well. is there something missing in this pattern?

Thank you so much for your reply,

lilyyan

0
Comment
Question by:lilyyan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 2000 total points
ID: 17800611
<script type="text/javascript">
// Allow spaces and letters only
function validate(frm){
var v=frm.n1.value;
var rx=/^[a-z\s]*$/i;
if(!rx.test(v)) {alert("Spaces and letters only"); frm.n1.focus(); return false;}
else alert("valid");
return false;
}

</script>
<form onsubmit="return validate(this)">
<input name=n1 value="abcd123">
<input type=submit value="Submit">
</form>
0
 

Author Comment

by:lilyyan
ID: 17800733
hi,

thanks for your reply. it works great!

I'm trying to understand the expression. i'm relative new to exprssion.

/^[a-z\s]*$/i;

i : means ignore the letter case ?

anything between / / is a pattern ?

[a-z\s ] means any input letter could be a-z or space?

* means 0 to any number

$ means ?

Thanks for your reply,

lilyyan
0
 
LVL 30

Expert Comment

by:callrs
ID: 17800766
i  - case insensitive
*  -  0 or more
+  -  1 or more
?   - 0 or 1, or it can mean non-greedy, or...
^  -  starts with, or inside square brackets it means NOT, e.g. [^a-z] means match if it not a-z
$  - ends with

http://www.experts-exchange.com/Q_21995615.html  repalce parameters
Getting started with Regular Expressions: http://www.experts-exchange.com/Q_21980291.html 
0
 

Author Comment

by:lilyyan
ID: 17800790
so /^[a-z\s]*$/i;  means the valid input is any letter (0 or more ) starts with a-z or space
and ends with  a-z or space. is this correct?

thanks for your reply!
0
 
LVL 30

Expert Comment

by:callrs
ID: 17801086
It's an expression that checks that a string starts and ends with ONLY 0 or more bytes of a-z A-Z space. If you had 'a1b', the test is false since the first 3 bytes ('a1b') has a 1, and the last 3 bytes (again 'a1b') has a 1.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question