Jump to content

Making a required field in registration.


JTA

Recommended Posts

Hello WHMCS users!

 

We're a Television Service Provider and not a hosting company.

 

Due to my countries amazing laws and regulations, I need the "Company" field required as I'm using it for their Social Security Number. (No * i promise, LOL)

Anyway, for me as a company I must require it for all online sales for residents on my country, so I was wondering if I could make it a required field for registrating? (I already renamed "Company" to "Kennitala",)

 

Some people aren't filling it out, and it's costing my company a lot of time and effort in calling each and everyone of them and getting the information we need. We try call the customers 2-3 times before we have to suspend their service until they call us or fill out the field.

 

So is this possible somehow?

 

Best regards,

Jon from Thor Telecom Iceland.

Link to comment
Share on other sites

Hi Jon,

 

Due to my countries amazing laws and regulations, I need the "Company" field required as I'm using it for their Social Security Number. (No * i promise, LOL)

Anyway, for me as a company I must require it for all online sales for residents on my country, so I was wondering if I could make it a required field for registration? (I already renamed "Company" to "Kennitala",)

Some people aren't filling it out, and it's costing my company a lot of time and effort in calling each and everyone of them and getting the information we need. We try call the customers 2-3 times before we have to suspend their service until they call us or fill out the field.

So is this possible somehow?

I should probably first mention that you should really have used a Client Custom Field for this purpose - you could have made it required in the admin settings and you wouldn't have needed to change your existing form fields to get the SSN from your customers. :idea:

 

however, I assume you don't want to change that now - so there would be a few ways to make a field required during registration.

 

if you want to do it in the checkout.tpl template, you could eithert add "required" to the Company form field...

 

<input type="text" name="companyname" id="inputCompanyName" class="field" placeholder="{$LANG.orderForm.companyName} ({$LANG.orderForm.optional})" value="{$clientsdetails.companyname}"{if $loggedin} readonly="readonly"{else} required{/if}> 

if you want to do it in jQuery instead, you could add the code below to the beginning of 'checkout.tpl'...

 

<script type='text/javascript'>
$(window).load(function(){
$(function(){
      $("#inputCompanyName").prop('required',true);
});
});
</script>

if you want to use the jQuery solution as an action hook, you could create a new .php file in /includes/hooks and paste the following code into it...

 

<?php

add_hook('ClientAreaHeadOutput', 1, function($vars) {

   if (App::getCurrentFilename()=='cart') {

   return "<script type='text/javascript'>
$(window).load(function(){
$(function(){
      $(\"#inputCompanyName\").prop('required',true);
});
});
</script>";
   }
});

Link to comment
Share on other sites

  • 3 years later...

Hey Brain,

I just tried your hook version and it does require it but the field still shows (optional).

Is there a way to change that, that you know of (we only work with business, so optional is really not what we want to show for a required field 😉 )

Link to comment
Share on other sites

Hi @Hein

3 hours ago, Hein said:

Is there a way to change that, that you know of (we only work with business, so optional is really not what we want to show for a required field 😉 )

there would be a handful of ways to do it - you could globally redefine, using Language Overrides, the $LANG.orderForm.optional language string for each language used - I think it's only used twice in the templates by default - company name and tax id form fields.

another way would be to modify the above hook and replace the placeholder text with just the company name language string and not include the optional language string...

<?php

# Cart Company Name Field Requirement Hook
# Written by brian!

function cart_company_name_requirement_hook($vars) {
	
	if ($vars['templatefile'] == 'viewcart') {
		$companyname = Lang::trans('orderForm.companyName');
		return <<<HTML
<script>$('#inputCompanyName').prop({'required':true,'placeholder':"$companyname"});</script>
HTML;
	}
}
add_hook("ClientAreaFooterOutput", 1, "cart_company_name_requirement_hook");

for completeness, I did post another hook last year that returns an error (in a custom language string) at checkout if the specified field is empty - it will show an error alert just like other required fields... if it wasn't for the "optional" part of your question, you could have used that hook - but your situation either requires the above hook to do both tasks, or you require the field with the hook below and modify the above hook to only change the placeholder.

Link to comment
Share on other sites

Hey  Brian,

We actually removed it from the template now but I hate doing that as any new update ... well, we know those issue all too well, right?! 🙂

And changing with language override does indeed only change 2 but the other one Can be optional (there are plenty of legit companies that do not have a VAT number 😉 )

So, a solution with Only changing the company one with a hook would be perfect.
I'll test and and let you know if that did it as I've seen the same question quite often before so I'm sure many would like simple and clean solution.

Link to comment
Share on other sites

10 minutes ago, Hein said:

We actually removed it from the template now but I hate doing that as any new update ... well, we know those issue all too well, right?! 🙂

yep - the more things change, the more they stay the same. 🙄

30 minutes ago, Hein said:

And changing with language override does indeed only change 2 but the other one Can be optional (there are plenty of legit companies that do not have a VAT number 😉 )

I didn't know if you needed/wanted to visibly tell users whether any fields are optional... I assume you're not telling them openly that some are required! 🙂

Link to comment
Share on other sites

  • 6 months later...

So the above options kinda worked for me, but I decided to use a different implementation by using the `ClientDetailsValidation` hook (to match native validation via PHP).  The ClientAreaFooterOutput is used to remove the (Optional) label:

https://gist.github.com/tripflex/9f0dcf4e8cc7c7a27aec41dad9629ac4

These forums suck so I had to post it on GitHub gist above.

The other option would be to use the above `ClientAreaFooterOutput` to set required prop when the "Create New Account" radio button is clicked.  This is required to remove the `required` prop when the user wants to login instead, otherwise it throws an HTML5 validation error on the company name field even if the user is just logging in:

add_hook( "ClientAreaFooterOutput", 1, function( $vars ){

	if ( $vars['templatefile'] == 'viewcart' || $vars['templatefile'] === 'clientregister' ) {
		$companyname = \Lang::trans( 'orderForm.companyName' );

		return <<<HTML
<script>jQuery( function($){
	function smyles_set_company_required( set_required ){
		var input_company_name = $('#inputCompanyName'); 
		if( input_company_name.length ){ 
			if( set_required ){
				input_company_name.prop({'required':true,'placeholder':"$companyname"}); 
			} else {
				input_company_name.removeAttr('required');
			}
		}
	}

	$('input[name="custtype"][value="new"]').on( 'ifChanged', function(){
		smyles_set_company_required( $(this).is(':checked') );
	});
	
	smyles_set_company_required( true );
	
	var company_label = $('label[for="inputCompanyName"]');
	if( company_label.length ){
		var clean_label = company_label.text().replace(/\(Optional\)/g, '');
		company_label.text( clean_label );
	}
});</script>
HTML;
	}
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated