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
https://forums.phpfreaks.com/topic/141245-solved-space-problem-validation/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.