简体   繁体   中英

SQL Plus Dot Net - Phone formatting tag not working

I'm using SQL+ dot net and really liking it so far, but I'm having trouble with the formatting for phone number. With the following code:

--+SqlPlusRoutine
    --&Author=Vincent Grazapoli
    --&Comment=Inserts a new Customer Billing Record and returns the new CustomerBillingId
    --&SelectType=NonQuery
--+SqlPlusRoutine
CREATE PROCEDURE [dbo].[CustomerBillingInsert]
(
    @CustomerBillingId int output,

--+Required
    @CustomerId int,

--+Required
--+StringLength=8,64
--+Email
    @Email varchar(64),

--+Required
--+StringLength=16,20
--+CreditCard
    @CreditCard varchar(20),

--+Required
--+StringLength=10,16
--+Phone
    @Phone varchar(16),

--...
-- other parameters

The email and credit card tags work as expected, but the phone number seems to only prevent letters, and allows numbers like 1234 etc. What am I doing wrong?

The generated code is as follows:

    public class CustomerBillingInsertInput
        {
            [Required(AllowEmptyStrings = false)]
            public int? CustomerId { set; get; }

            [EmailAddress]
            [DataType(DataType.EmailAddress)]
            [Required(AllowEmptyStrings = false)]
            [StringLength(64, MinimumLength = 8)]
            public string Email { set; get; }

            [CreditCard]
            [DataType(DataType.CreditCard)]
            [Required(AllowEmptyStrings = false)]
            [StringLength(20, MinimumLength = 16)]
            public string CreditCard { set; get; }

            [Phone]
            [DataType(DataType.PhoneNumber)]
            [Required(AllowEmptyStrings = false)]
            [StringLength(16, MinimumLength = 10)]
            public string Phone { set; get; }

//Other properties

            /// <summary>
            /// Use this method to validate the instance.
            /// If the method returns false, the ValidationResults list will be populated.
            /// </summary>
            public bool IsValid()
            {
                ValidationResults = new List<ValidationResult>();
                return Validator.TryValidateObject(this, new ValidationContext(this), ValidationResults, true);
            }

            /// <summary>
            /// ValidationResults populated from the IsValid() call.
            /// </summary>
            public List<ValidationResult> ValidationResults{ set; get; }
        }
}
}

So I received a response from sqlplus.net feedback and the answer they gave me was as follows, might help someone even if you're not using sql+ dot net.

The --+Phone tag is doing what is expected, however, the logic for that annotation in c# is quite liberal since it has to apply globally. For your case you can do one of the following: Use the --+RexExPattern tag along with an appropriate supplemental tag for the error message like so.

--+Required
--+StringLength=10,16
--+Phone
--+RegExPattern=^[0-9]{10,12}$
    --&ErrorMessage=Phone is not a vaid format.
    @Phone varchar(16),

That would translate to the following data annotation.

[RegularExpression(@"^[0-9]{10,12}$",ErrorMessage = "Phone is not a vaid format.")]

Or, and this would be my preference, leave your semantic tags the way they are, and use a service like twilio to send a text with a verification code. Have your user confirm that verification code on a subsequent form post, and you are golden.

Confirming the phone number, or email for that matter, is really the only way to be sure, and since it looks like you are persisting customer billing information, it would be worth the extra work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM