It looks like you're new here. If you want to get involved, click one of these buttons!
<fieldset> <label for="email">Email</label> <input type="text" name="email" id="email" onblur="validateEmail(value)" /> <span id="emailError" style="display: none;">You must enter a valid email address</span> <label for="Name">Name</label> <input type="text" name="name" id="name" onblur="validateName(value)" /> <span id="nameError" style="display: none;">Please enter your full name.</span> <label for="Name">Phone number</label> <input type="text" name="phone" id="phone" onblur="validatePhone(value)" /> <span id="phoneError" style="display: none;">Please provide a valid phone number.</span> </fieldset>
Comments
You can get around this by defining those functions in a way that gives them global scope and one was to do this is to assign them to your own global namespace. ... you should then be able to call the global function like so:
Thanks for your reply. I tried it your way, but I am still getting errors, which I can´t quite figure out how to solve.
So here is what I put in into the Story Javascript section (I reckon the drama starts here): (I took the code from here)
The passage with the form field reads:
The validateEmail function seems to be called correctly onblur, but the correspondent <span> doesn't show. The validateName function doesn´t work. (error: "undefined is not a function")
Also, I have a big doubt at this point: Can I assign the field values to variables (which I can later use in Sugarcube) from within the validation-functions (WITHOUT a submit button)?
Thanks a lot,
richVIE
There are a couple of issues with your code/markup.
1. Your existing Javascript code:
a. The if condition (typeof window.My == "undefined") of your second block of code will always return false because the My namespace was defined in the first block of code, which is why your validateName function is not getting defined.
b.. Your functions are defining parameters (value) which are never used within the related function.
c. Your functions are referencing variables (email and x) which are not defined.
The following Javascript define a My namespace, as well as the validateEmail and validateName functions (which are slightly modified versions of your own code to take advantage of two new parameters and may still contain errors.)
2. The parameters you are passing to the functions within the passage markup.
Your first input element (ID'ed name) is passing the value of the element's name property but the related function accepts it as a parameter named value, which it never uses internally. The second input element (ID'ed email) is passing the value of the element's value property into a parameter named value which again is never used.
The following passage content has been change to take advantage of the functions defined in point 1.
Is there a way to assign the form values to variables I can work with in Sugarcube WITHIN the validation functions (without a submit button)?
I tried after the check input part, and wanted to use $name in another passage. Didn't work.
Assuming you have pre-initialised a variable named $name you can assign a value to it within your Javascript code like so: