Jump to content

[SOLVED] Space Problem, validation


XpertWorlock

Recommended Posts

I'm trying to kill two birds with one stone, when you type in the input, if you type an illegal character, it deletes it on you, and if you click the button, it runs the script, and only follows through if it comes back false.

 

It works perfectly, doing what it's supposed to do, but I wanted to allow it to have spaces, the only problem is, that, if you click the button, it allows the illegal characters and doesn't stop.

 

<html>

<head>
<script type="text/javascript">

//NEED TO BE ABLE TO ALLOW ONE SPACE 
function illegalCharacters(id)
{
var illegalChars = /\W/;

if (illegalChars.test(id.value))
{
id.value = (id.value.slice(0, -1))
return true;
}
else
{
return false;
}
}


function updateDiv(id)
{
if (illegalCharacters(id) == true)
{
return;
}
document.getElementById('div').innerHTML = id.value
}

</script>
</head>
<body>


<input id="testBox" type"text" onkeyup="illegalCharacters(testBox)">
<input type="button" value="Click" onclick="updateDiv(testBox)">


<div id="div" style="border:1px solid black;width:200px; padding:20px;"></div>
</body>


</html>

 

If you try

/\W /

instead, you will see what I mean.

 

Also I have another question, since this is a hacked version of someone else's illegal character script, how come it has to be

 

if (illegalChars.test(id.value))

and not

if (illegalChars(id.value))

??

 

Thanks

Mike

Link to comment
Share on other sites

The regexp for \W or space is

 

/[\W ]/

 

The regexp for \W followed by space is

 

/\W /

 

About the syntax, illegalChars is an object.  You can call an object's methods, but you cannot call an object itself.  "test" is a method of illegalChars.

 

As an analogy, you could say btherl.eat(burger), but not btherl(burger), otherwise I wouldn't know what to do with the burger.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.