Solved

javascript only a-z A-Z and while space

Posted on 2006-10-24
5
325 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

724 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