Validation on textfield

I have a simple textfield to capture the player character's name as {charname}:

[[Character name]]:
What is your character's name? 

<label>Character name</label>
<input type="text" id="character_name">
[[Select]](page2)

[[page2]]:
    squiffy.set("charname", jQuery("#character_name").val());
    squiffy.set("charname", jQuery("#character_name").val().replace(/(^|\W)\w/, function(t) { return t.toUpperCase() })); 

Hi, {charname}.

There's no validation to force a user to complete the field, so they can go through the whole story with {charname} set to nothing. I'd like to detect if no value's been entered and send the user back to [[Character name]], but I can't find a way of doing it. {if charname=:} doesn't work, nor {if charname=null:}

Can anyone advise on how best to validate?


What about...

{if charname="":}

Just guessing this one.


Thanks Rohana - that doesn't work for me either :(


This will...

[[Character name]]:
What is your character's name? 

<label>Character name</label>
<input type="text" id="character_name">

[[Select]](page2)

[[page2]]:
    squiffy.set("charname", jQuery("#character_name").val());
    squiffy.set("charname", jQuery("#character_name").val().replace(/(^|\W)\w/, function(t) { return t.toUpperCase() })); 

    //added section
    set("charFlg",true);
    if (jQuery("#character_name").val()==""){set("charFlg",false);}


Hi, {charname}.

{charFlg}

Give it a shot.


With this, you should be able to test for empty fields via the boolean charFlg.


His Bluevoss - thanks so much for this!

It nearly gets me there! If the user fails to enter a name the first time, they're sent back to [[Character name]]: to try again - however, even if they enter a name this second time, the {if charFlg:}check still finds {charFlg} false so they just loop round and round, unable to successfully enter a name on any but the first try. Can you advise what I'm getting wrong?


Lucky you. I actually fussed with this a while back and remembered the trick. So here's a working copy.

[[Character name]]:
@clear
What is your character's name? 

<label>Character name</label>
<input type="text" id="character_name">

[[Select]](page2)

[[page2]]:
    squiffy.set("charname", jQuery("#character_name").val());
    squiffy.set("charname", jQuery("#character_name").val().replace(/(^|\W)\w/, function(t) { return t.toUpperCase() })); 

    //added section
    set("charFlg",true);
    if (jQuery("#character_name").val()==""){set("charFlg",false);}


{if not charFlg: 

Woops. I didn't quite hear that. Can you [[try again?]](Character name)

}

{else:
Hi, {charname}.

}

Now, a critical deal - when you loop back, you need to clear the page (specifically, right when [[Character name]] is called. This resets the box totally and makes it available for entry. I found this after I took out my clear statement (I generally always use them) and suddenly my name entry DIDN'T work.

My other earlier option was that if charFlg was false, set the character name to "Nameless Stranger" but I think this is better :)


Absolutely spot on now. Thank you so much - I've only been messing with Squiffy for a couple of days, and I've worked out some stuff by trial and error, but it's invaluable having a forum with experts to come running to! I'll probably have more questions in due course :)) Thanks again!


I should point out that:

    set("charFlg",true);
    if (jQuery("#character_name").val()==""){set("charFlg",false);}

is the same as

    set("charFlg", jQuery("#character_name").val() != "");

and almost the same as

    set("charFlg", !!jQuery("#character_name").val());

(the latter version is faster, using the !! convert-to-boolean shortcut, but I think it will also exclude the name "0")


For the record, my java scripting is pretty much "lifeboat coding". I'm not very comfortable with it and pretty much monkey-see, monkey-do it. But those solutions were pretty slick.


This topic is now closed. Topics are closed after 180 days of inactivity.

Support

Forums