...

View Full Version : only a-z, A-Z, 0-1,_,- available



UrbanTwitch
08-08-2008, 09:10 PM
ok so in my registration form i want user's name can only be filled with:

a-z, A-Z, 0-9, - , _

any other character will give the user an error.
How do I do this?

Trinithis
08-09-2008, 01:00 AM
if(!/\w\-_/.test(theText)) {
bad
}
else {
good
}

UrbanTwitch
08-09-2008, 01:05 AM
err explain?

ninnypants
08-09-2008, 01:16 AM
if(!/\w\-_/.test(theText)) {
bad
}
else {
good
}


It should be


if(/\w\-_/.test(theText)) {
good
}
else {
bad
}

@MysticScript
What is happening is you define a regular expression(/\w-_/) containing the characters you want to allow that is compared to the users input if the input tests true and only has those characters then it passes and will execute the script normally (good) or it fails and does something else that you've defined (bad)

UrbanTwitch
08-09-2008, 02:44 AM
Can you put it in this:

<input name='username' type='text'>


:-)

ninnypants
08-09-2008, 02:59 AM
Yeah you can but you would have to make it a function first like so:


function checkInput(el){
if(/\w\-_/.test(el.value)) {
good
}else {
bad
}
}


<input name='username' type='text' onchange="checkInput(this);" />

Philip M
08-09-2008, 08:37 AM
Yeah you can but you would have to make it a function first like so:


function checkInput(el){
if(/\w\-_/.test(el.value)) {
good
}else {
bad
}
}


<input name='username' type='text' onchange="checkInput(this);" />

Sorry, but Trinithis is entirely correct.
But \w means a-z A-Z 0-9 and underscrore, so _ is redundant.
What about names with an apostrophe such as O'Neill? And I don't know of any proper names containing digits.

The regex is not correct as it stands as if(/\w\-_/.test(el.value)) { means "if the value of el includes any (single) character from the range a-zA-Z0-9 or hyphen or underscore then return true (good)". So for example ***a&&& returns true as the letter a is found.

What is wanted is the converse-

If the value of el includes any (single) character other than a-zA-Z-_' then return false (bad).

So I would suggest:-

function
checkInput(el) {
if (!/[a-z]\-\_\'/gi.test(el.value)) { // ! means not i means ignore case
bad (return false) }
else {
good (return true which is the default so the part in green is not necessary).
}
}


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

UrbanTwitch
08-09-2008, 06:46 PM
Wait so what happens if they get it wrong and return falses happens?

Also, is this a good example?


<html><title>Hi</title>

<head>
<script>
checkInput(el) {
if (!/[a-z]\-\_\'/gi.test(el.value)) { // ! means not i means ignore case
bad (return false) }
else {
good (return true which is the default so the part in green is not necessary).
}
}
</script>
</head>
<body>
<input name='username' type='text' onchange="checkInput(el);" /><br><br><input value='Submit' type='submit'>
</body>
</html>

ninnypants
08-09-2008, 06:49 PM
You can use it to stop the form from being submitted until they conform to the standard that you created

Thanks Philip M for correcting me

Philip M
08-09-2008, 10:55 PM
function checkInput(el) {

onchange = "checkInput(this)"

If the test fails and the regex returns false then you need to have something like:-


alert ("Invalid entry. Try again!");
document.formname.elementname.value = ""; // clear the text box
document.formname.elementname.focus(); // re-focus on the box
return false;

Trinithis
08-09-2008, 11:01 PM
I forgot to surround my regex with "[^" "]". Aka:


/[^\w\-_]/


But as Philip pointed out, the underscore is redundant. If you want to make it smaller, you can take out the \ before the - in this case too.

Philip's regex is incorrect as well. (Guess it wasn't our lucky days!)

My new regex:


if(!/[^\w-]/.test(theText)) {
bad
}
else {
good
}

Arbitrator
08-10-2008, 12:07 AM
Below are completely functional examples that do the job. My regular expression (/^[A-z0-9\-_]+$/) is slightly different from the previous ones.

Live:

HTML: http://www.jsgp.us/demos/cf146246-check-username-character-content.html.php
XHTML: http://www.jsgp.us/demos/cf146246-check-username-character-content.xhtml.php


HTML:

<!doctype html public "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="application/ecmascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Demo for CodingForums.com Thread 146246 (HTML)</title>
<meta name="Author" content="Patrick Garies">
<meta name="Created" content="2008-08-09">
<meta name="Revised" content="2008-08-09">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
</style>
<!--[if IE]>
<style type="text/css" media="all">
h1 { zoom: 1; filter: alpha(opacity=80); }
</style>
<![endif]-->

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<form id="sign-up" method="post" action="">
<div>
<p id="username_restrictions">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>
<div id="username_group">
<label for="username">Select a Username</label>
<input id="username" name="username" type="text" size="36">
</div>
<div>
<input type="submit" value="Submit Username">
</div>
</div>
</form>
<!--[if !IE]>-->
<script type="application/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
</script>

<!--<![endif]-->
<!--[if IE]>
<script type="text/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.returnValue = false;
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.returnValue = false;
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.attachEvent("onsubmit", check_username);
username.attachEvent("onchange", check_username);
</script>
<![endif]-->

</body>
</html>

XHTML:

<?xml version="1.0" encoding="UTF-8"?>

<html xml:lang="en-Latn" xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"></meta>
<meta http-equiv="Content-Script-Type" content="application/ecmascript"></meta>
<meta http-equiv="Content-Style-Type" content="text/css"></meta>
<title>Demo for CodingForums.com Thread 146246 (XHTML)</title>
<meta name="Author" content="Patrick Garies"></meta>
<meta name="Created" content="2008-08-09"></meta>
<meta name="Revised" content="2008-08-09"></meta>
<style type="text/css" media="all"><![CDATA[
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
]]></style>

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<form id="sign-up" method="post" action="">
<div>
<p id="username_restrictions">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>
<div id="username_group">
<label for="username">Select a Username</label>
<input id="username" name="username" type="text" size="36"></input>
</div>
<div>
<input type="submit" value="Submit Username"></input>
</div>
</div>
</form>
<script type="application/ecmascript" defer="defer"><![CDATA[
var d = document;
var XHTML_NS = "http://www.w3.org/1999/xhtml";
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElementNS(XHTML_NS, "p");
username_error.setAttributeNS(null, "id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
]]></script>

</body>
</html>

HTML with PHP:

<?php
ob_start();
header("Content-Type: text/html; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
?>
<!doctype html public "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="application/ecmascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Demo for CodingForums.com Thread 146246 (HTML)</title>
<meta name="Author" content="Patrick Garies">
<meta name="Created" content="2008-08-09">
<meta name="Revised" content="2008-08-09">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
</style>
<!--[if IE]>
<style type="text/css" media="all">
h1 { zoom: 1; filter: alpha(opacity=80); }
</style>
<![endif]-->

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<?php
$validity_code = 0;
$error_message = "";
import_request_variables("p", "FRV_");
if (is_string($FRV_username) && $FRV_username === "") {
$validity_code = 1;
$error_message = "\n\t\t\t\t\t<p id=\"username_error\">Your username must be, at least, one character in length!</p>";
}
else if (is_string($FRV_username) && preg_match("/^[A-z0-9\-_]+$/", $FRV_username) === 0) {
$validity_code = 2;
$error_message = "\n\t\t\t\t\t<p id=\"username_error\">The username " . $FRV_username . " contains one or more invalid characters!</p>";
}
else if (is_string($FRV_username)) {
$validity_code = 3;
echo "<p id=\"username_confirmation\">Thank you. Your username, " . $FRV_username . ", has been successfully submitted!</p>";
}

if ($validity_code < 3) {
echo "<form id=\"sign-up\" method=\"post\" action=\"\">
<div>
<p id=\"username_restrictions\">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>
<div id=\"username_group\">
<label for=\"username\">Select a Username</label>
<input id=\"username\" name=\"username\" type=\"text\" size=\"36\">"
. $error_message . "
</div>
<div>
<input type=\"submit\" value=\"Submit Username\">
</div>
</div>
</form>";
}
?>

<!--[if !IE]>-->
<script type="application/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
</script>
<!--<![endif]-->
<!--[if IE]>
<script type="text/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.returnValue = false;
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.returnValue = false;
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.attachEvent("onsubmit", check_username);
username.attachEvent("onchange", check_username);
</script>
<![endif]-->

</body>
</html>

XHTML with PHP:
(Note: The forum keeps reformatting the second \n\t\t\t\t\t into nttttt for some reason.)

<?php
ob_start();
header("Content-Type: application/xhtml+xml; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<html xml:lang="en-Latn" xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"></meta>
<meta http-equiv="Content-Script-Type" content="application/ecmascript"></meta>
<meta http-equiv="Content-Style-Type" content="text/css"></meta>
<title>Demo for CodingForums.com Thread 146246 (XHTML)</title>
<meta name="Author" content="Patrick Garies"></meta>
<meta name="Created" content="2008-08-09"></meta>
<meta name="Revised" content="2008-08-09"></meta>
<style type="text/css" media="all"><![CDATA[
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
]]></style>

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<?php

$validity_code = 0;
$error_message = "";
import_request_variables("p", "FRV_");
if (is_string($FRV_username) && $FRV_username === "") {
$validity_code = 1;
$error_message = "\n\t\t\t\t\t<p id=\"username_error\">Your username must be, at least, one character in length!</p>";
}
else if (is_string($FRV_username) && preg_match("/^[A-z0-9\-_]+$/", $FRV_username) === 0) {
$validity_code = 2;
$error_message = \n\t\t\t\t\t<p id=\"username_error\">The username " . $FRV_username . " contains one or more invalid characters!</p>";
}
else if (is_string($FRV_username)) {
$validity_code = 3;
echo "<p id=\"username_confirmation\">Thank you. Your username, " . $FRV_username . ", has been successfully submitted!</p>";
}

if ($validity_code < 3) {
echo "<form id=\"sign-up\" method=\"post\" action=\"\">
<div>
<p id=\"username_restrictions\">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>
<div id=\"username_group\">
<label for=\"username\">Select a Username</label>
<input id=\"username\" name=\"username\" type=\"text\" size=\"36\"></input>"
. $error_message . "
</div>
<div>
<input type=\"submit\" value=\"Submit Username\"></input>
</div>
</div>
</form>";
}
?>

<script type="application/ecmascript" defer="defer"><![CDATA[
var d = document;
var XHTML_NS = "http://www.w3.org/1999/xhtml";
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[A-z0-9\-_]+$/.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElementNS(XHTML_NS, "p");
username_error.setAttributeNS(null, "id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
]]></script>

</body>
</html>

UrbanTwitch
08-10-2008, 09:04 AM
WOW thanks!

Hmm in HTML with PHP.

Does


<?php
ob_start();
header("Content-Type: text/html; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
?>
have to be at very top? Also... in the available characters can you add the SPACE also?

Philip M
08-10-2008, 09:57 AM
My regular expression (/^[A-z0-9\-_]+$/) is slightly different from the previous ones.

I think you intend:-

(!/^[A-z0-9\-_]+$/) // ! means not. That is, only the specified characters and no others are allowed. Your regex returns true if any of the specified characters are found in the string - which is quite different.

One more time, proper or personal names do not contain digits, and you need to allow for names which contain an apostophe such as O'Reilly.

Also A-z is not exactly the same as A-Za-z or a-z/i. The range A-z includes the ASCII characters 91-96 inclusive which are [/]^_ and grave accent. Presumably these are not desired. The correct regex is:-

if (/[^a-z\s\-\_\']/gi.test(theText.value)) {

That is, if any character other than those specified is found anywhere in the string, return true.

As Arbitrator says, additional checks are needed to strip leading spaces and check that the field length is greater than zero:-

theText.value = theText.value.replace(/^\s+/,"");
if (theText.value.length == 0 ) {
alert ("You must fill in a user name");
return false
}



I now realise that the OP wants "user name" or screen name, which may include digits, (such as Philip99) as opposed to personal or proper name. So Abritrator's regex (as amended) (!/^[a-z0-9\-_]+$/i) is fine.

My version of the regex is:-

if (/[^a-z0-9\s\-\_]/gi.test(theText.value)) {

That is, if any character other than those specified is found anywhere in the string, return true.
But presumably leading or trailing spaces are not allowed so a preliminary test is necessary:-

if (/(^\s)|(\s$)/.test(theText.value)) {
alert ("Your user name may not begin with or end with a space");
return false;
}



Haikus are easy
But sometimes they don't make sense.
Refrigerator.

Arbitrator
08-13-2008, 09:18 AM
Does


<?php
ob_start();
header("Content-Type: text/html; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
?>
have to be at very top? Also... in the available characters can you add the SPACE also?Based upon what Iíve read, ob_start() needs to be at the top. The purpose of ob_start() is so that the header function calls need not be the first things in the document; this is particularly useful if you encode your documents as UTF-8 with a byte order mark (BOM) since the BOM causes problems otherwise. Presumably, use of ob_start() would allow you to move the header function calls even further into the document.


I think you intend:-

(!/^[A-z0-9\-_]+$/) // ! means not.If you look at my code, youíll see that Iíve used the not operator in the appropriate places. I didnít use it in the particular sentence that you quoted since that operator is (A) not a part of the regular expression and (B) not used when I use that regular expression in the PHP portion of the code.


Also A-z is not exactly the same as A-Za-z or a-z/i. The range A-z includes the ASCII characters 91-96 inclusive which are [/]^_ and grave accent. Presumably these are not desired.I wasnít aware of this; those additional characters were not desired. Iíll correct the demos (but not the post) when my server comes back up and Iíve verified your assertion to be correct (not that I really doubt it).

Arbitrator
08-16-2008, 05:59 AM
Iíll correct the demos (but not the post) when my server comes back up and Iíve verified your assertion to be correct (not that I really doubt it).As promised, the demos have been updated to incorporate the suggested regular expression (/^[a-z0-9\-_]+$/i). I also made some minor, aesthetic changes to the code as well. The updated ďwith PHPĒ code is shown below.

HTML with PHP:

<?php
ob_start();
header("Content-Type: text/html; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
?>
<!doctype html public "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="application/ecmascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Demo for CodingForums.com Thread 146246 (HTML)</title>
<meta name="Author" content="Patrick Garies">
<meta name="Created" content="2008-08-09">
<meta name="Revised" content="2008-08-15">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
</style>
<!--[if IE]>
<style type="text/css" media="all">
h1 { zoom: 1; filter: alpha(opacity=80); }
</style>
<![endif]-->

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<?php
$validity_code = 0;
$error_message = "";
import_request_variables("p", "FRV_");
if (is_string($FRV_username) && $FRV_username === "") {
$validity_code = 1;
$error_message = "\t\t\t\t\t<p id=\"username_error\">Your username must be, at least, one character in length!</p>\n";
}
else if (is_string($FRV_username) && preg_match("/^[a-z0-9\-_]+$/i", $FRV_username) === 0) {
$validity_code = 2;
$error_message = "\t\t\t\t\t<p id=\"username_error\">The username " . $FRV_username . " contains one or more invalid characters!</p>\n";
}
else if (is_string($FRV_username)) {
$validity_code = 3;
echo "<p id=\"username_confirmation\">Thank you. Your username, " . $FRV_username . ", has been successfully submitted!</p>";
}
if ($validity_code < 3) {
echo
"<form id=\"sign-up\" method=\"post\" action=\"\">\n"
. "\t\t\t<div>\n"
. "\t\t\t\t<p id=\"username_restrictions\">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>\n"
. "\t\t\t\t<div id=\"username_group\">\n"
. "\t\t\t\t\t<label for=\"username\">Select a Username</label>\n"
. "\t\t\t\t\t<input id=\"username\" name=\"username\" type=\"text\" size=\"36\">\n"
. $error_message
. "\t\t\t\t</div>\n"
. "\t\t\t\t<div>\n"
. "\t\t\t\t\t<input type=\"submit\" value=\"Submit Username\">\n"
. "\t\t\t\t</div>\n"
. "\t\t\t</div>\n"
. "\t\t</form>"
;
}
?>

<!--[if !IE]>-->
<script type="application/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[a-z0-9\-_]+$/i.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
</script>
<!--<![endif]-->
<!--[if IE]>
<script type="text/ecmascript" defer="defer">
var d = document;
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.returnValue = false;
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[a-z0-9\-_]+$/i.test(username.value)) {
event.returnValue = false;
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElement("p");
username_error.setAttribute("id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.attachEvent("onsubmit", check_username);
username.attachEvent("onchange", check_username);
</script>
<![endif]-->

</body>
</html>

XHTML with PHP:

<?php
ob_start();
header("Content-Type: application/xhtml+xml; charset=UTF-8");
header("Content-Script-Type: application/ecmascript");
header("Content-Style-Type: text/css");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<html xml:lang="en-Latn" xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"></meta>
<meta http-equiv="Content-Script-Type" content="application/ecmascript"></meta>
<meta http-equiv="Content-Style-Type" content="text/css"></meta>
<title>Demo for CodingForums.com Thread 146246 (XHTML)</title>
<meta name="Author" content="Patrick Garies"></meta>
<meta name="Created" content="2008-08-09"></meta>
<meta name="Revised" content="2008-08-15"></meta>
<style type="text/css" media="all"><![CDATA[
* { margin: 0; padding: 0; }
html, h1, input { font: 16px/1.2 sans-serif; }
html { background: #ded url("bg_c.png"); color: black; }
h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
a { color: black; }
p { padding: 1em; }
label::after { content: ":"; }
*#username_restrictions { margin: 1em 1em 0; background: #ddd; }
*#username_group { margin: 0 1em; padding: 1em; background: #eee; }
*#username_group + div { margin: 1em 3em 1em 1em; text-align: right; }
*#username_error { margin: 1em 0 0; border: 1px solid; background: crimson; color: palegoldenrod; font-weight: bolder; }
*#username_confirmation { margin: 1em; background: forestgreen; color: palegoldenrod; font-weight: bolder; }
]]></style>

</head>
<body>

<h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146246">CodingForums.com Thread 146246</a></h1>
<?php
$validity_code = 0;
$error_message = "";
import_request_variables("p", "FRV_");
if (is_string($FRV_username) && $FRV_username === "") {
$validity_code = 1;
$error_message = "\t\t\t\t\t<p id=\"username_error\">Your username must be, at least, one character in length!</p>\n";
}
else if (is_string($FRV_username) && preg_match("/^[a-z0-9\-_]+$/i", $FRV_username) === 0) {
$validity_code = 2;
$error_message = "\t\t\t\t\t<p id=\"username_error\">The username " . $FRV_username . " contains one or more invalid characters!</p>\n";
}
else if (is_string($FRV_username)) {
$validity_code = 3;
echo "<p id=\"username_confirmation\">Thank you. Your username, " . $FRV_username . ", has been successfully submitted!</p>";
}
if ($validity_code < 3) {
echo
"<form id=\"sign-up\" method=\"post\" action=\"\">\n"
. "\t\t\t<div>\n"
. "\t\t\t\t<p id=\"username_restrictions\">Usernames may contain the characters A&#xa0;through&#xa0;Z, a&#xa0;through&#xa0;z, 0&#xa0;through&#xa0;9, -, and _ and must be, at least, one character in length.</p>\n"
. "\t\t\t\t<div id=\"username_group\">\n"
. "\t\t\t\t\t<label for=\"username\">Select a Username</label>\n"
. "\t\t\t\t\t<input id=\"username\" name=\"username\" type=\"text\" size=\"36\"></input>\n"
. $error_message
. "\t\t\t\t</div>\n"
. "\t\t\t\t<div>\n"
. "\t\t\t\t\t<input type=\"submit\" value=\"Submit Username\"></input>\n"
. "\t\t\t\t</div>\n"
. "\t\t\t</div>\n"
. "\t\t</form>"
;
}
?>

<script type="application/ecmascript" defer="defer"><![CDATA[
var d = document;
var XHTML_NS = "http://www.w3.org/1999/xhtml";
var signup = d.getElementById("sign-up");
var username = d.getElementById("username");
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[a-z0-9\-_]+$/i.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
else {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").parentNode.removeChild(d.getElementById("username_error"));
}
}
};
function generate_username_error(error_message) {
if (Boolean(d.getElementById("username_error"))) {
d.getElementById("username_error").firstChild.data = error_message;
}
else {
var username_error = d.createElementNS(XHTML_NS, "p");
username_error.setAttributeNS(null, "id", "username_error");
username_error.appendChild(d.createTextNode(error_message));
username.parentNode.appendChild(username_error);
}
}
signup.addEventListener("submit", check_username, false);
username.addEventListener("change", check_username, false);
]]></script>

</body>
</html>

Philip M
08-16-2008, 07:29 AM
function check_username(event) {
if (username.value === "") {
event.preventDefault();
generate_username_error("Your username must be, at least, one character in length!");
}
else if (!/^[a-z0-9\-_]+$/i.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}


You have not noticed that the OP wanted that a space be in the list of available characters. However, spaces at the start or end of the string should be disallowed.

So a better validation is:-



function check_username(event) {
var minlen = 5; // minumum permitted length of username
username.value = username.value.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
if (username.value.length < minlen) {
event.preventDefault();
generate_username_error("Your username must be, at least, " + minlen + " characters in length!");
}
else if (!/^[a-z0-9\s\-_]+$/i.test(username.value)) {
event.preventDefault();
generate_username_error("The username " + username.value + " contains one or more invalid characters!");
}
"The world is made up of five basic elements. Earth, air, fire, water and bull****". - Personal mantra

Before a man speaks it is safe to assume that he is a fool. After he speaks it is seldom necessary to assume.
H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist

Arbitrator
08-16-2008, 09:11 AM
You have not noticed that the OP wanted that a space be in the list of available characters. I have now; the mentioned issue is fixed:


I added spaces to the regular expressions.
I indicated that a space is a valid character in the instructive paragraph.
I used the white-space: pre-wrap declaration (with -moz-pre-wrap and pre as fallback values) to ensure that spaces are not collapsed in error and confirmation messages.
I added word-wrap: break-word in one of the conditional comments so that Internet Explorer can wrap text when white-space: pre is in effect (since it doesnít support white-space: pre-wrap).

Philip M
08-16-2008, 12:29 PM
I indicated that a space is a valid character in the instructive paragraph.

One more time, it surely makes no sense to allow a space at the start or the end of the user-name.

UrbanTwitch
08-18-2008, 04:17 AM
I am very grateful for this quality support. Thing is. I have no idea on how I would put this on my page. (surely you and I don't want this to go to waste!) If any of you have any spare time and would like to help me finishing this:

Put the script in here?


<?php include("/home/jsfdan/public_html/includes/vtop.php"); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Register new account - sodaDome.com</title>
<link rel="shortcut icon" href="http://sodadome.com/favicon.ico">
<meta name="Description" content="About Me.">
<meta name="Keywords" content="sodaplay, sodarace, soda ltd, physics, education, science, gravity, mass, friction, javascript_freek, dan jasnowski, sodaplay, sodarace, sodaconstructor, models, moovl, ed burton, soda">

<?php include("/home/jsfdan/public_html/temp2/includes/titleheader.php"); ?>

<script language = "Javascript">
/**
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function ValidateForm(){
var emailID=document.frmSample.email

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
</script>

</head>

<?php include("/home/jsfdan/public_html/temp2/includes/greenlinks.php"); ?>

<?php include("/home/jsfdan/public_html/temp2/includes/leftmenu.php"); ?>

<!-- START BODY CONTENT -->
<br><br>

<?php
$date = date("F j, Y");
echo "";
//checks if there trying to veriy there account
if(isset($_GET['verify'])) {
//gets the code and makes it safe
$code = addslashes($_GET['code']);
//gets the code from the database
$getcode=mysql_query("SELECT * FROM `verification` WHERE `code` = '$code'");
//counts the number of rows
$getcode = mysql_num_rows($getcode);
//if the ammount of rows is 0 the code does not exist
if($getcode == 0) {
echo "Invalid verification code!";
}
//or if the code does exist we will activiate there account
else{
//get the data from the database
$getcode=mysql_query("SELECT * FROM `verification` WHERE `code` = '$code'");
//fetchs the data from the db
$dat = mysql_fetch_array($getcode);
//sets the users user level to 2 which means they can now use there account
$update = mysql_query("UPDATE `members` SET `userlevel` = '2' WHERE `username` = '".$dat['username']."'") or die(mysql_error());
//deletes the code as there is no use of it now
$delete = mysql_query("DELETE FROM `verification` WHERE code = '$code'");

//says thanks and your account is ready for use
echo "Thank you, Your account has been verified.";
}
}else
//if we have posted the register for we will register this user
if(isset($_GET['register'])) {
//check to see if any fields were left blank
if((!$_POST[username]) || (!$_POST[password]) || (!$_POST[cpassword]) || (!$_POST[email])) {
echo "A field was left blank please go back and try again.";
}else {
//posts all the data from the register form
$username = $_POST[username];
$password = $_POST[password];
$cpassword = $_POST[cpassword];
$email = $_POST[email];
//check see if the 2 passwords are the same
if($password == $cpassword)
{
//encrypts the password 8 times
$password = sha1(md5(md5(sha1(md5(sha1(sha1(md5($password))))))));
$cname = mysql_query("SELECT `username` FROM `members` WHERE `username` = '$username'");
$cname= mysql_num_rows($cname);
//checks to see if the username or email allready exist
$check=array(
1 => "****",
2 => "****",
3 => "*****",
4 => "****",
5 => "***",
6 => "****",
7 => "Asshole",
8 => "Jackass",
9 => "*****",
10 => "Vagina",
11 => "Pussy",
12 => "Weed",
13 => "******",
14 => "KKK",
15 => "Whore",
16 => "Admin",
17 => "Mod",
18 => " ",
19 => "-"
);
foreach($check as $c){
if(ereg(''.$c.'+', $username)){
$detected = "True";
}
}
if($detected == 'True')
{
echo "Bad username detected, Please use a different username.";
}
else
if($cname>=1) {
echo "The username is already in use";
}elseif(strlen($_POST['username']) > 15) {
echo "Your name is too long, please change it. <a href='register.php'>Go back.</a>";
}elseif(strlen($_POST['username']) < 2){
echo "Your name is too short, please change it. <a href='register.php'>Go back.</a>";
}else{
//gets rid of bad stuff from there username and email
$username = addslashes(htmlspecialchars($username));
$email = addslashes(htmlspecialchars($email));

if($semail == "1") { // $email set as 1 means email activation is active
//adds them to the db
$adduser = mysql_query("INSERT INTO `members` (`username`, `password`, `email`) VALUES('$username','$password','$email')");
//posible letters for the verification code
$alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//shuffles the letters around to create a 16 long code
$code = substr(str_shuffle($alphanum), 0, 16);
//adds there code along with there user name to the db
$addcode = mysql_query("INSERT INTO `verification` (`username`, `code`) VALUES('$username','$code')");
//don't edit this, this is the link for there activication
$link = "http://$host$self?verify&code=$code";
//sends the email to the person
mail("$email", "Member-Ship Validation", "Thank you for registering on $sitename.
Please copy the below link into you address bar,

$link", "From: Site Verification");
//message sent now lets tell them to check there email
echo "You are now registered,<br><br>Please check your email to activate your account.";
}else{ //no need for email activation
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
$getsitestats = mysql_query("SELECT * FROM `sitesettings` WHERE `name` = 'register'");
$stats = mysql_fetch_array($getsitestats);
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("F j, Y, g:i a");
//$adduser = mysql_query("INSERT INTO `members` (`username`, `password`, `email`, `userlevel`,`ip`, `signupdate`) VALUES('$username','$password','$email','2','$ip','$date')");
//start welcome message
//start welcome message
$to = $username; //get who it is to
$from = 'Bot'; //who its from
$subject = "Welcome to sodaDome.com"; //new subject
$msg = "Hello, $username, welcome to <b>sodaDome.com!</b><br>This site contains the latest news for sodaplay and gallery by UrbanTwitch of his models. sodaDome hopes you have a nice day and thank you for registering! Here are some links to help you while you are here!<br><br><b>&#187;</b> <a href=http://sodadome.com/editprofile.php>Edit your profile</a> - Editing your profile - tells us who you are!<br><b>&#187;</b> <a href=http://sodadome.com/changepassword.php>Change your password</a> - Changing your password, if you ever need too.<br><b>&#187;</b> <a href=http://sodadome.com/bugreport.php>Bug Report</a> - Help sodaDome stay free of bug errors by reporting them here.<br><b>&#187;</b> <a href=http://sodadome.com/inbox.php>Private Message Inbox</a> - Send, recieve, and save messages to and from users.<br><b>&#187;</b> <a href=http://sodadome.com/my_warns.php>My Warns</a> - This lets you see all the warnings you have recieved.<br><b>&#187;</b> <a href=http://sodadome.com/my_friends.php>Friendlist</a> - Keep track of friends.<br><b>&#187;</b> <a href=http://sodadome.com/blocklist.php>Blocklist</a> - Block those pesky people who bother you!<br><b>&#187;</b> <a href=http://sodadome.com/report.php>Report</a> - Report users who have done something wrong.<br><b>&#187;</b> <a href=http://sodadome.com/settings.php>User Settings</a> - Set your user settings to your needs.<br><b>&#187;</b> <a href=http://sodadome.com/avatar.php>Avatar</a> - Upload your own picture that defines you.<br><br>Thats about it. There is more to explorer. If you need any futher help do not be afraid to contact <a href=http://sodadome.com/members.php?page=msgto&user=1>Dan</a>!<br><br>- Bot"; //the content
$date = date("F j, Y, g:i a"); //the date sent
$do = mysql_query("INSERT INTO `privates` (`to`,`from`,`date`,`subject`,`content`) VALUES ('" . $to . "','" . $from . "','" . $date . "','" . $subject . "','" . $msg . "')") or die(mysql_error());
//end welcome message
//end welcome message
echo "You are now registered.<br><br>You can now <a href='login.php'>login</a> to your account";
unset($_SESSION['security_code']);
} else {
echo "Sorry invalid code. Try again.";

}
}
}
}else{
echo "Your password and conformation password do not match!";
}
}
}else{
$getsitestats2 = mysql_query("SELECT * FROM `sitesettings` WHERE `name` = 'register'");
$cta = mysql_fetch_array($getsitestats2);
//none of the above so lets show the register form
echo "<div id='title'>Register a new account! $cta[name]</div><br>"; if ($cta[set] == 'off') { echo "<font color=red><B>This form is currently disabled for bug testing. Please check back tomorrow.</b></font>"; } echo "
<style>
#sign-up {
background:#D9E1C8;
border: 1px solid #98C738;
width: 360px;
padding: 4px;
}
#reg {
background: #A4BCDD;
width: 490px;
border: 1px solid #265599;
}
table {
background: transparent;
width: 490px;
}
</style>

If you register an account you can access a lot of cool features. To see a list <a href='http://sodadome.com/features.php'><b>go here</b></a>.<br><br>
<form style='width:490px;' id='sign-up' name='frmSample' action='register.php?register' method='post' onSubmit='return ValidateForm()'>
<table style='width:490px;'>
<tr>
<td align=right>Username:</td>
<td><input type='text' name='username' size='30' maxlength='15' onchange=\"CheckItems(this.value, 'user');\"></td><td style='width:30px;' align=center><span id=\"user_results\"></span></td> <tr>
<td align=right>Email:</td>
<td><input type='text' name='email' size='30' maxlength='30' onchange=\"CheckItems(this.value, 'email');\"></td><td style='width:30px;' align=center><span id=\"email_results\"></span></td>
</tr>
<tr>
<td align=right>Password:</td>
<td><input type='password' name='password' size='30' maxlength='15'></td>
</tr>
<tr>
<td align=right>Confirm Password:</td>
<td><input type='password' name='cpassword' size='30' maxlength='15'></td>
</tr>
<tr><td align=right>
<img src='CaptchaSecurityImages.php?width=100&height=40&characters=5' />
</td><td><input size='30' id='security_code' name='security_code' type='text' />
</tr></td>
<tr>
<td colspan='3' align=right><input type='submit' id='reg' value='Register'></td>
</tr>
</table>
</form>";
}
echo "";
?>


<br />

<!-- END BODY CONTENT -->

<?php include("/home/jsfdan/public_html/temp2/includes/footer.php"); ?>

(I am truly sorry if any of the names in the 'bad names' array list offend you.)

Arbitrator
08-18-2008, 05:39 AM
One more time, it surely makes no sense to allow a space at the start or the end of the user-name.I am aware that you deem that functionality necessary, but I never saw that functionality requested by MysticScript and, thus, did not feel like coding it. As it is, I believe that you already coded that functionality in post #17 so thereís not really a need for me to bother. If you want to demonstrate it integrated into a complete document, feel free to amend my existing code and post it here or onto whatever Web host that you use.


I am very grateful for this quality support. Thing is. I have no idea on how I would put this on my page. (surely you and I don't want this to go to waste!) If any of you have any spare time and would like to help me finishing this:Multiple people here have demonstrated techniques that you might use; if you donít plan to learn from them and just want ad hoc code that you can copy and paste (as Iíve inferred from the ďno ideaĒ part of your comment), then any help that I offer will be pointless. Iíll have to pass.

(In any case, the effort hasnít gone to waste since I learned some things myself in writing the demo.)

Philip M
08-18-2008, 08:39 AM
It is painfully obvious that you are indeed trying to run before you can walk, and have little idea of what you are trying to do. You seem to have bolted on validation scripts which are far from apposite and which are never called. Here is a pointer towards what you need but you must make some effort yourself - or take up underwater motorcycling as an alternative passtime.

First of all, get rid of this junk:-



<script language = "Javascript">
/**
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}

return true
}

function ValidateForm(){
var emailID=document.frmSample.email

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
</script>


Then here is the code you need to validate username and email (not tested so look out for typos):-



<td><input type='text' name='username' size='30' maxlength='15' onchange=\"CheckName(this);\"></td><td style='width:30px;' align=center><span id=\"user_results\"></span></td> <tr>
<td align=right>Email:</td>
<td><input type='text' name='email' size='30' maxlength='30' onchange=\"CheckEmail(this);\"></td><td style='width:30px;' align=center><span id=\"email_results\"></span></td>
</tr>
<tr>


<script type = "text/javascript">

function checkName(un) {
if (/(^\s)|(\s$)/g.test(un.value)) {
alert ("Your user name may not begin with or end with a space - please re-enter");
un.value = "";
un.focus();
return false;
}
if (/[^a-z0-9\s\-\_]/gi.test(un.value)) {
alert ("Your user name contain invalid characters - try again");
un.value = "";
un.focus();
return false;
}
}

function checkEmail(em) {
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em.value))) {
alert ("Invalid email address - try again!);
em.value = "";
em.focus();
return false
}

</script>


Most members of this forum do try to assist newcomers. But unfortunately some others are more interested in showing off how extremely clever they are.



A child of five would understand this. Send someone to fetch a child of five.
Groucho Marx

Arbitrator
08-19-2008, 03:47 AM
Most members of this forum do try to assist newcomers. But unfortunately some others are more interested in showing off how extremely clever they are.Since Philip enjoys antagonizing me at every turn (including via PMs), Iím going to go ahead and entertain him by replying to this:

He is correct: I do enjoy showing off my ďclevernessĒ. However, I also like to see the people that Iím trying to help learn so that they can write their own clever code unassisted. I concluded that the OP (MysticScript) isnít very interested in learning and, therefore, that Iíd rather expend my volunteer time elsewhere (perhaps helping others). (If the solution to his last question were easier (i.e., less time consuming) or I expected to learn something from performing such a task, I wouldnít mind as much and might have provided a solution anyway.)

Anyway, a solution has been provided, so I guess this is all moot. Iíll leave any further requests in this thread to our resident expert, Philip M.

UrbanTwitch
08-20-2008, 12:20 AM
Sometimes I don't learn by doing but copy/pasting and dissecting scripts.

To be fairly honest.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum