The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Validation of string difficulties
Discuss Validation of string difficulties in the JavaScript Development forum on Dev Shed. Validation of string difficulties JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 4th, 2008, 10:30 AM
|
|
Contributing User
|
|
Join Date: Jan 2008
Posts: 139
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
|
|
|
Validation of string difficulties
Hi, I was given some brilliant help on this forum with my last post so thought I would try again.
I have this code:
Code:
<html>
<head>
<script type="text/javascript">
function Prompt_hello()
{
var name=prompt("Please enter your name","");
try
{
if name (!isNaN)
throw=err1;
else if (name=null)
throw err2;
else if (name="")
throw err3;
}
catch (er)
{
if (er=="err1")
alert("Please only enter valid characters");
if (er=="err2" || er=="err3")
alert("Please enter a name");
}
//if (name!=null && name!="")
{
alert("Hi there, " + name);
}
}
</script>
</head>
<body>
<input type="button" onclick="Prompt_hello()" value="Hello! Please Enter your name" />
</body>
</html>
As you can see, I am basically wanting a prompt to appear so the user can enter a name and have an alert box appear greeting them with their name. This worked with no problem when just using the simple validation below:
Code:
if (name!=null && name!="")
However, I thought I would make it a bit more complex, just as a learning matter than any real need, and decided to use, Catch...try and throw validation. I now cannot get it to work.
I figure I am either using the try, catch functions wrongly, or, and I guess this is most likely, the character validation function is incorrect.
I saw the !isNaN on another script when looking online and thought I would give it a go, as the other validation scripts are far to advanced for me at the moment, so I wanted something simple for what should be a simple script. The purpose was to heck that only characters, a-z, have been used. (any other ways to validate this would also be great, as I wanna learn all the alternatives).
If anyone can point me in the right direction, or let me know where I have gone wrong, I would be very grateful.
Cheers, Mike
|

January 4th, 2008, 11:01 AM
|
|
|
isNaN is a function so you have to give it something to check
Code:
(isNaN(name)==1) //Name is blank
|

January 5th, 2008, 03:22 AM
|
 |
4:04 Time Not Found
|
|
Join Date: Jan 2004
Location: Northern Virginia
|
|
I RyanT's solution is good .. though how about revamping your code a bit..
Code:
function Prompt_hello() {
var name=prompt("Please enter your name","");
var err;
if (name.match(/^\d+$/) || name.match(/^[\W\D]+$/)) {
// Name only contains numbers.
// OR
// Name only contains non-word and non-digit characters
err = 'Please only enter valid characters.';
} else if (name=='') {
// name is empty
err = 'Please enter a name.';
}
if (err.length>0) {
alert(err);
// Re-run the function, if you want.
// Prompt_hello();
} else {
alert("Hi there, " + name);
}
}
__________________
I am so smart, I am so smart, S.M.R.T ... I mean S.M.A.R.T.
Stop Using Pop-Ups
|

January 7th, 2008, 03:40 AM
|
|
Contributing User
|
|
Join Date: Jan 2008
Posts: 139
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
|
|
|
Tried your suggstions
hi vbrtrmn, Thanks for the code, but it doesn't fully work. Even when I enter a name it still throws me an error. "please enter only valid characters." But thanks for the validation part, I have seen it coded that way in scripts online.
1. Do you always have to use the name.match(/^[\W\D]+$/) twice, once for numbers and characters, or will once do?
2. Are those characters in the parenthesis fixed, or can you add/subtract them depending on what you will allow?
Here is my code as it looks now:
Code:
<html>
<head>
<script type="text/javascript">
function Prompt_hello()
{
var name=prompt("Please enter your name:","");
try
{
if (!isNaN(name)==1) //Name is blank
throw "err1";
else if (name=null)
throw "err2";
else if (name="")
throw "err3";
else
alert("Hi there, " + name);
}
catch (er)
{
if (er=="err1")
alert("Please only enter valid characters err1");
if (er=="err2")
alert("Please enter a name err2");
if (er=="err3")
alert("Please enter a name err3");
//if (name!=null && name!="")
}
}
</script>
</head>
<body>
<input type="button" onclick="Prompt_hello()" value="Hello! Please Enter your name" />
</body>
</html>
I have added RyanT's line of code.
I brought the "alert("hi there..." part in to the if...else section as it was being called regardless of being thrown an error. I also made the err2 and err 3 separate lines of code.
Probs:
1: entering invalid characters such as @ stills gives the "Hi there..." alert
2. leaving the name blank or entering numbers throws err1 but no others, no biggie, I can just have the first err as a general one
3. name variable is not passing to the alert box. It comes up "hi there, "
Thanks for all the help I have received so far,
Mike
|

January 7th, 2008, 12:28 PM
|
 |
4:04 Time Not Found
|
|
Join Date: Jan 2004
Location: Northern Virginia
|
|
|
I didn't test my code very well... per your problems, can you describe the "rules" you want to meet for a name? For instance which characters are valid?
A-Z,a-z,0-9,@,???
What else describes a name:
-Cannot contain just numbers
-Cannot be empty or just contain spaces
-???
|

January 8th, 2008, 05:55 AM
|
|
Contributing User
|
|
Join Date: Jan 2008
Posts: 139
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
|
|
|
Well, it is just basic validation. The name should basically only be A-z, not be blank or null. Just I can add @ at the mo and the alert still shows, so it is accepted using the validation rules you gave me.
The name variable is also not passing to the alert, so have to sort that out to. what does err.length>0 mean? does the length object not measure a strings length? I am a complete JS noob, so excuse all the questions.
Thanks, Mike
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|