Forums

[SOLVED] HOW: Check a field and show a error message.

monak83 15 Dec, 2011
I have a field like this:
<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 440px;">Numero di copie (stesso soggetto)</label>
    <input class="cf_inputbox required validate-number" maxlength="150" size="5" title="Inserisci un valore numerico corretto" id="text_1" name="n_copie" type="text" onChange="updatethis(this.form);" />
  <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
				<div class="tooltipdiv">Numero di copie (stesso soggetto) :: Inserisci il numero (in cifre) di copie dello stesso sogetto</div>
  </div>
  <!--<div class="cfclear"> </div>-->
</div>


If I insert a char I have a error message because the field has "validate-number" and is shown "Inserisci un valore numerico corretto" and so I can't do the order.

Can I show a different message if the field is less than a number (example: value insert < 2 show "Insert a number bigger than 2")?
How
Thanks so much
monak83
monak83 16 Dec, 2011
Thanks so much Bob for you reply but I have seen the post but is not similat to my case and I don't know very well javascript!😶
Can you post for me the code I must to insert?

IMPORTANT: on your method I have also the features like "required" or "validate-number" that block the send form (insert to cart) if they aren' correct (empty or char)?

regards
monak83
monak83 16 Dec, 2011
Hi Bob,
I have seen the file jsvalidation2.js where ther are some function to validete fields of a form.
For example in chronoforms to validate a field if is a number the system (chronoforms) add class "validate-number".
If the field has this class the system use this code:


if(field.hasClass('validate-number')){
 if( tmessage) { var message_validate_number = tmessage; }
 name.add( Validate.Numericality, { 
 notANumberMessage: message_validate_number 
});


So I think that if I make a condition (like the code I have posted) called (for example) "major_of_10" and so I add this class to my field in this way:

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 440px;">Altezza (in cm)</label>
    <input class="cf_inputbox required validate-number major_of_10" maxlength="150" size="5" title="Inserisci un valore numerico corretto" id="text_3" name="altezza" type="text" onChange="updatethis(this.form);" value="" />
  <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
				<div class="tooltipdiv">Altezza (in cm) :: Inserisci la dimensione in centimetri dell' ALTEZZA del tuo Banner</div>
  </div>
</div>


can be a solution to validate my field?
If yes, how can I write a condition that validate my field if is MAJOR OF 10?

I hope you understand me also my bad english.

I'm waiting for your reply.

regards
monak83
GreyHead 16 Dec, 2011
Hi monka83,

Your are correct - the post I linked to was validating length. But if you go to the LiveValidation page from the link there you will find the Numericality validation and that included maximum and minimum values. The code to use it will be very similar.

Bob
monak83 16 Dec, 2011
Hi bob so Is correct to add a code in the jsvalidation2.js file?

I tried to add this:

if(field.hasClass('more10')){
				if( tmessage) { var message_validate_more10 > tmessage; }
				name.add( Validate.Numericality, { 
					notMore10Message: message_validate_more10 
				});
				fieldsarray[fieldsarray_count] = name;
				fieldsarray_count = fieldsarray_count + 1;
			}


but dosen't works.
Can you help me please?
Regards and thanks so much for your time!!
GreyHead 16 Dec, 2011
Hi monak83,

You shouldn't need to edit the ChronoForms js files at all. The example I quoted was written to go into the Form JavaScript box. (Notice that you left out the first and last lines which might cause problems.)


Bob
monak83 16 Dec, 2011
Sorry Bob,
can you write me the code I should pt on my javascript file?
Sory for my insistance but I dont' know how can do this!!!
Thanks so much
monak83
monak83 19 Dec, 2011
Hi Bob,
sorry for my new question but I can't go forward without this controll.
Can you tell my what code I should insert to lock the form if a field is minor of a value?
Thanks so much and sorry for my insistance.
Regards and thanks so much for your wonderfoul help!!!
monak83
monak83 20 Dec, 2011
Hi Bob,
try try and try I have DONE!!!
Thanks so much for you support.

I post the code for someone that have this problem.


 window.addEvent('domready', function(){
  var valid = new LiveValidation('text_4');
  valid.add(Validate.Numericality, { minimum: 10, failureMessage: "CUSTOM ERROR MESSAGE" });
  });


THERE IS A PROBLEM: why I can't seen the failureMessage and I see the default error message?
How can I show a customaized error message?

thanks so much
monak83
monak83 21 Dec, 2011
find the solution also for Custom Validation message: use not failureMessage but tooLowMessage.

So the code will be:

window.addEvent('domready', function(){
  var valid = new LiveValidation('text_4');
  valid.add(Validate.Numericality, { minimum: 10, tooLowMessage: "CUSTOM ERROR MESSAGE" });
  });


Thanks so much to Bob for his wonderful works!
regards
monak83
GreyHead 21 Dec, 2011
Hi monak83,

Well done :-) Good to see that it's working.

Bob
This topic is locked and no more replies can be posted.