Closed Thread Icon

Topic awaiting preservation: Form validate characters (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=24915" title="Pages that link to Topic awaiting preservation: Form validate characters (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Form validate characters <span class="small">(Page 1 of 1)</span>\

 
redroy
Bipolar (III) Inmate

From: 1393
Insane since: Dec 2003

posted posted 02-04-2005 17:40

How would I make it so that a text field is required to have a minimum amount of characters, at least 2 numeric characters, and 1 special? I've been searching the net for a script similar that I could just change around but I can't find anything... thanks for any help.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 02-04-2005 18:10

On submit of the form you should test the value of your field with the pattern (.*\d.*\d.*[list_of_specials_chars].*|.*\d.*[list_of_specials_chars].*\d.*|.*[list_of_specials_chars].*\d.*\d.*) If it doesn't match it, cancel the sumbit process.

Do you need more insights ?

redroy
Bipolar (III) Inmate

From: 1393
Insane since: Dec 2003

posted posted 02-04-2005 18:18

Yes... more insights... Thanks!

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 02-04-2005 20:17

sorry.

The cabalistic thing I wrote above is a RegEx to match if a string has at least 2 digits and one special characters, in any order. Once you translate my previous post in code, you have :


<form onsubmit="return this.elements['fieldToCheck'].value.match( /(.*\d.*\d.*[_,\.\-\$\*].*|.*\d.*[_,\.\-\$\*].*\d.*|.*[_,\.\-\$\*].*\d.*\d.*)/gi )?true:false">

<input type="text" name="fieldToCheck" value="" />
<input type="submit" value="submit" />

</form>


Replace the [_,\.\-\$\*] parts in the code above by the list of the special characters want you to authorize between square braquets. Make sure to escape these characters appropriately.

redroy
Bipolar (III) Inmate

From: 1393
Insane since: Dec 2003

posted posted 02-05-2005 00:18

Rad! it works.... so could I put that whole string in an 'if' function to have an alert box pop-up when invalid...? I would have to take out the '?true:false' and set it == to true or false, right? Sorry, I'm just learning this stuff. Thanks for your help!

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 02-05-2005 01:25

Sure, and to so it'd be more convenient to put the whole form check in a function that you'd call in the onsubmit.

<form onsubmit="return checkForm( this )">
... blah blah, the rest of the form

<script type="text/javascript">

function checkForm( formHandle )
{
if( formHandle.elements['fieldToCheck'].value.match( /(.*\d.*\d.*[_,\.\-\$\*].*|.*\d.*[_,\.\-\$\*].*\d.*|.*[_,\.\-\$\*].*\d.*\d.*)/gi ) )
return true

alert( "/!\ warning a field is incorrect /!\" )
return false
}

</script>

redroy
Bipolar (III) Inmate

From: 1393
Insane since: Dec 2003

posted posted 02-05-2005 03:53

Poi, you ROCK! Thanks for taking the time to help me out. Peace.

« BackwardsOnwards »

Show Forum Drop Down Menu