3

I really need to have custom or even magento validation for my input field.

Problem is, there is no form in my field, there is only input field.

<input id="item_custom_payment_" name="pay_part_payment" value="" style="" class="input-text item-price validate-greater-than-zero">
<button .... >Submit</button> //calling custom controller action

For example: Comment section in order view page in admin area.

This var customForm = new VarienForm('custom_form'); doesn't work because there is no <form> in my section.

Is there anyway around to validate the input field?

UPDATE

My button looks like this:

<button id="id_5bb6f3992cb320bf703ed93f702dd64a" title="Submit Paid Amount" type="button" class="scalable save paid-amount" onclick="submitAndReloadArea($('order_paid_block_wrap').parentNode, '[admin_url]'); javascript:reload(); return true" style=""><span><span><span>Submit Paid Amount</span></span></span></button>
  • You can validate input field using jquery – ND17 Jan 22 '16 at 4:40
7

You can validate individual field like below

Validation.validate($('your-element-id'));

Refer this link

0

If your input isn't inside any other form you just can wrap it with a new <form>, then you'll be able to use VarienForm for validation.

0

Try this:

<input id="item_custom_payment" name="pay_part_payment" value="" style="" class="input-text">
<div style="display: none;" id="validation_item_custom_payment" class="validation-advice">This is a required field.</div>

<button id="id_5bb6f3992cb320bf703ed93f702dd64a" title="Submit Paid Amount" type="button" class="scalable save paid-amount" onclick="submitAndReloadArea($('order_paid_block_wrap').parentNode, '[admin_url]'); javascript:reload(); return true" style=""><span><span><span>Submit Paid Amount</span></span></span></button>

<script>
function submitAndReloadArea() {
    var itemCustomPayment = $('item_custom_payment').value;
    if(itemCustomPayment=='') {
        $('validation_item_custom_payment').show();
        return false;
    }else {
       $('validation_item_custom_payment').hide();
    }
}
</script>
  • This will work, however there is "submitAndReloadArea" function called too. Is there a way to prevent? I was looking for more like magento way of doing this btw. – Adarsh Khatri Jan 22 '16 at 5:16
  • updated ans pls check now – Abdul Jan 22 '16 at 5:21
  • Or Can you share submitAndReloadArea() code? – Abdul Jan 22 '16 at 5:21
  • submitAndReloadArea() is magento core function. So there is nothing we can put inside if(validation). – Adarsh Khatri Jan 22 '16 at 5:23
  • updated ans pls check now – Abdul Jan 22 '16 at 5:28
0

It did not work with JQuery for me, but worked with Javascript instead.

Example:

HTML

<input type="text" class="required-entry input-text" name="medium-description" id="medium-description" />

Javascript:

<script type="text/javascript">

isMediumDescriptionValid = Validation.validate(document.getElementById('medium-description'))

if(isMediumDescriptionValid){
  //success action
}else{
  //failure action
}

</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.