Jump to content

Javascript Validation


andrewgarn

Recommended Posts

I have done a bit of this before but just to check the length of a field or field present, bit rusty on it now.

 

I want to validate a username field and bring an error if the username contains a space or a hyphen.

 

So far I have:

 

<form name="register" onsubmit="return validateUsername(this)" method="post" action="register.php">

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

<input type="password" name="password">

<input type="text" name="email" maxlength="100">

<input type="submit" name="submit" value="Submit">

</form>

 

<script type="text/javascript">

function validateUsername(fld) {

var error = "";

var illegalChars = [something here]

if (illegalChars.test(username)) {

fld.style.background = 'Yellow';

    error = "The username is invalid, you must use an underscore to represent a space.\n";

return error;

}

</script>

 

Help would be great. Thanks :)

 

Link to comment
Share on other sites

Change the submit button to a regluar button, and add a onclick event to that button. Then use javascript to validate whatever you want. Then if everything is good, just submit the form with javascript

Link to comment
Share on other sites

Change the submit button to a regluar button, and add a onclick event to that button. Then use javascript to validate whatever you want. Then if everything is good, just submit the form with javascript

 

Adam, I'm not mening to bash you, but that is the exact opposite of how you should perform validation. The reason is that if the user does not have JS enabled they are unable to use the form at all.

 

You should ALWAYS be validating on the server-side anyway. But, having validation client-side is a nice feature to have as well. You just don't weant to make your site unusable for non-js users. You can elegantly support both with the following method.

 

Use a standard submit button, and in the FORM tag include validation through an onsubmit trigger such as onsubmit="return validate()". The validate() function should return false if validation fails and the form won't be submitted. That way if a user has JS enabled they will get the benefit of client-side validation. However, if a user does not have JS enabled the form will still submit and any errors will be taken care of in server-side validation.

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.