gebruikersnaam lengte controleren
Ik zit met een probleem ik probeer doormiddel van een ajax request te controleren of de naam niet te kort of te lang is.
Hieronder het script
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
h1>Registration</h1>
<div class="site">
<script type="text/javascript">
$(document).ready(function()
{
$("#username").blur(function() { // when focus out
$("#message").html('<img src="images/loading.gif" />');
var form_data = {
action: 'check_username',
username: $(this).val()
};
$.ajax({
type: "POST",
url: "/pages/check_user.php",
data: form_data,
success: function(result) {
$("#message").html(result);
}
});
});
$("#reg_email").blur(function() {
$("#message_mail").html('checking username...');
var form_data = {
action: 'check_email',
reg_email: $(this).val()
};
$.ajax({
type: "POST",
url: "/pages/check_user.php",
data: form_data,
success: function(result) {
$("#message_mail").html(result);
}
});
});
});
</script>
<form method="post" name="form1" id="form1" action="">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" id="username" maxlength="11" value="" autocomplete="off" />
<div id="message"></div></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="reg_password" maxlength="15" /></td>
</tr>
<tr>
<td>Repeat. Password:</td>
<td><input type="password" name="reg_confirmpw" /></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><input type="text" name="reg_email" id="reg_email" autocomplete="off" />
<div id="message_mail"></div></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="reg_submit" value="Register" /></td>
</tr>
</table>
</form>
</div>
<div class="site">
<script type="text/javascript">
$(document).ready(function()
{
$("#username").blur(function() { // when focus out
$("#message").html('<img src="images/loading.gif" />');
var form_data = {
action: 'check_username',
username: $(this).val()
};
$.ajax({
type: "POST",
url: "/pages/check_user.php",
data: form_data,
success: function(result) {
$("#message").html(result);
}
});
});
$("#reg_email").blur(function() {
$("#message_mail").html('checking username...');
var form_data = {
action: 'check_email',
reg_email: $(this).val()
};
$.ajax({
type: "POST",
url: "/pages/check_user.php",
data: form_data,
success: function(result) {
$("#message_mail").html(result);
}
});
});
});
</script>
<form method="post" name="form1" id="form1" action="">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" id="username" maxlength="11" value="" autocomplete="off" />
<div id="message"></div></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="reg_password" maxlength="15" /></td>
</tr>
<tr>
<td>Repeat. Password:</td>
<td><input type="password" name="reg_confirmpw" /></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><input type="text" name="reg_email" id="reg_email" autocomplete="off" />
<div id="message_mail"></div></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="reg_submit" value="Register" /></td>
</tr>
</table>
</form>
</div>
Dan de PHP script
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$action = $_POST['action'];
if($action == 'check_username')
{
$u = $_POST['username'];
_check_username($u);
}
if($action == 'check_email')
{
$e = $_POST['reg_email'];
_check_email($e);
}
function _check_username($u)
{
if($_POST['username'] == "" )
{
echo '<font color="red"><span class="no"><strong></strong>This cannot be an empty field!</span></font>';
}
else
{
if(strlen($_POST['username'] < 4))
{
echo 'te kort';
}
else
{
echo 'ok';
}
}
}
if($action == 'check_username')
{
$u = $_POST['username'];
_check_username($u);
}
if($action == 'check_email')
{
$e = $_POST['reg_email'];
_check_email($e);
}
function _check_username($u)
{
if($_POST['username'] == "" )
{
echo '<font color="red"><span class="no"><strong></strong>This cannot be an empty field!</span></font>';
}
else
{
if(strlen($_POST['username'] < 4))
{
echo 'te kort';
}
else
{
echo 'ok';
}
}
}
Deze code boven zou een ok melding moeten geven als de username meer dan 4 tekens bevat.
Echter is het zo dat wat ik ook doe hij blijft aangeven dat het te kort is.
Wie kan mij in de goede richting duwen.
Met vriendelijke groet,
Thomas de vries
Doe eens een print_r($_POST);
Dit is de output
te kort Array ( [action] => check_username [username] => testing )
Hetzelfde probleem doet zich voor als ik toch ga posten
Dit is de php code die het checked mocht js uitvallen
Code (php)
1
2
3
4
2
3
4
elseif($_POST['username'] == "" || strlen($_POST['username'] < 4) || strlen($_POST['username'] > 15 ))
{
echo '<div class="fail">Youre username must contain between 4 and 15 signs</div>';
}
{
echo '<div class="fail">Youre username must contain between 4 and 15 signs</div>';
}
Gewijzigd op 16/08/2012 22:48:34 door thomas de vries
Je haakjes staan verkeerd.
strlen($_POST['username'] < 4)
moet zijn
strlen($_POST['username']) < 4
En die ander idem.
Dank je voor je snelle reactie SanThe.